Iterate hashmap in java

Example package com.w3schools;   import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set;   public class Test { public static void main(String args[]){ //Create HashMap object. Map<Integer, String> hashMap = new HashMap<Integer, String>();   //Add objects to the HashSet. hashMap.put(4, "Roxy"); hashMap.put(2, "Sunil"); hashMap.put(5, "Sandy"); hashMap.put(1, "Munish"); hashMap.put(3, "Pardeep");   //Print the HashMap object. System.out.println("HashMap elements:"); … Read more

Create hashmap in java

Example package com.w3schools;   import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set;   public class Test { public static void main(String args[]){ //Create HashMap object. Map<Integer, String> hashMap = new HashMap<Integer, String>();   //Add objects to the HashSet. hashMap.put(4, "Roxy"); hashMap.put(2, "Sunil"); hashMap.put(5, "Sandy"); hashMap.put(1, "Munish"); hashMap.put(3, "Pardeep");   //Print the HashMap object. System.out.println("HashMap elements:"); … Read more

Avoid duplicate user defined objects in TreeSet

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeSet;   class Employee{ private String name; private int salary; private int id;   public Employee(int id, String name, int salary){ this.id = id; this.name = name; this.salary = salary; }   public String getName() { return name; } public void setName(String name) { this.name = name; } … Read more

Get highest value element from a set

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeSet;   class Employee{ private String name; private int salary;   public Employee(String name, int salary){ this.name = name; this.salary = salary; }   public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public … Read more

Get least value element from a set

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeSet;   class Employee{ private String name; private int salary;   public Employee(String name, int salary){ this.name = name; this.salary = salary; }   public String getName() { return name; } public void setName(String name) { this.name = name; } public int getSalary() { return salary; } public … Read more

Find duplicate value in an array in java

Simplest way to find duplicate entries in an array is to add array entries to the TreeSet. As treeset does not support duplicate entries, we can easily find out duplicate entries. Example package com.w3schools;   import java.util.TreeSet;   public class Test { public static void main(String[] args) { //Create String Array String[] strArray = {"Jai", … Read more

Remove duplicate entries from an array in java

Simplest way to remove duplicate entries from an array is to pass array entries to the TreeSet constructor. Syntax: new TreeSet(tmpList); Example package com.w3schools;   import java.util.Arrays; import java.util.List; import java.util.TreeSet;   public class Test { public static void main(String[] args) { //Create String Array String[] strArray = {"Jai", "Mahesh", "Hemant", "Hemant", "Mahesh"};   //Convert … Read more

List to treeset in java

We can convert list into treeset by passing list into the TreeSet constructor. Syntax: new TreeSet(list); Example package com.w3schools;   import java.util.ArrayList; import java.util.List; import java.util.TreeSet;   public class Test { public static void main(String[] args) { //Create list List<String> list = new ArrayList<String>(); list.add("Jai"); list.add("Mahesh"); list.add("Vishal"); list.add("Hemant"); System.out.println("List elements: "+list);   //Convert list into … Read more

Iterate treeset in java

Example package com.w3schools;   import java.util.Iterator; import java.util.TreeSet;   public class Test { public static void main(String[] args) { //Create TreeSet object. TreeSet<String> treeSet = new TreeSet<String>();   //Add objects to the HashSet. treeSet.add("Jai"); treeSet.add("Mahesh"); treeSet.add("Hemant"); treeSet.add("Vishal"); treeSet.add("Naren");   //Print the TreeSet object. System.out.println("TreeSet elements:"); System.out.println(treeSet); //Print the TreeSet elements using iterator. Iterator<String> iterator=treeSet.iterator(); System.out.println("TreeSet … Read more

Create treeset in java

Example package com.w3schools;   import java.util.Iterator; import java.util.TreeSet;   public class Test { public static void main(String[] args) { //Create TreeSet object. TreeSet<String> treeSet = new TreeSet<String>();   //Add objects to the HashSet. treeSet.add("Jai"); treeSet.add("Mahesh"); treeSet.add("Hemant"); treeSet.add("Vishal"); treeSet.add("Naren");   //Print the TreeSet object. System.out.println("TreeSet elements:"); System.out.println(treeSet); //Print the TreeSet elements using iterator. Iterator<String> iterator=treeSet.iterator(); System.out.println("TreeSet … Read more