Create linkedhashmap in java

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

Reverse sort keys in a treemap

We can use descendingMap() method to reverse the map object in java. Syntax: treeMap.descendingMap(); Example package com.w3schools;   import java.util.Map; import java.util.TreeMap;   public class Test { public static void main(String args[]){ //Create TreeMap object. TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();   //Add objects to the TreeMap. treeMap.put(4, "Roxy"); treeMap.put(2, "Sunil"); treeMap.put(5, "Sandy"); treeMap.put(1, … Read more

Get last key element from treemap in java

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeMap;   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 first key element from treemap in java

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeMap;   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

Sort keys in treemap by using comparator

Example package com.w3schools;   import java.util.Comparator; import java.util.TreeMap;   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

Remove all elements from a treeMap in java

We can use clear() method to remove all elements from a treemap in java. clear(): Removes all key-value pairs from this map. Syntax: public void clear() Example package com.w3schools;   import java.util.TreeMap;   public class Test { public static void main(String args[]){ //Create TreeMap object. TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();   //Add objects … Read more

Get entryset from treemap in java

We can use entrySet() method to get entry set from treemap in java. entrySet(): Returns a Set that contains the entries of this map. The set contains objects of type Map.Entry. It provides a set-view of this map. Syntax: public Set entrySet() Example package com.w3schools;   import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap;   public class … Read more

Get all keys from treemap in java

We can use keySet() method to get all keys from treemap in java. keySet(): Returns a Set that contains the keys in this map. This method provides a set-view of the keys in this map. Syntax: public Set keySet() Example package com.w3schools;   import java.util.Set; import java.util.TreeMap;   public class Test { public static void … Read more

Search a value in treemap in java

We can use containsValue() method to search a value in treemap in java. containsValue(Object v): Returns true if this map contains specified value otherwise returns false. Syntax: public boolean containsValue(Object v) Example package com.w3schools;   import java.util.TreeMap;   public class Test { public static void main(String args[]){ //Create TreeMap object. TreeMap<Integer, String> treeMap = new … Read more

Search a key in treemap in java

We can use containsKey() method to search a key in treemap in java. containsKey(Object k): Returns true if this map contains specified key otherwise returns false. Syntax: public boolean containsKey(Object k) Example package com.w3schools;   import java.util.TreeMap;   public class Test { public static void main(String args[]){ //Create TreeMap object. TreeMap<Integer, String> treeMap = new … Read more