Copy map content to another treemap

We can use putAll() method to copy map content to another treemap. putAll(Map m): Puts all the entries from m into this map. Syntax: public void putAll(Map m) 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>();   … Read more

Iterate treemap in java

Example package com.w3schools;   import java.util.Iterator; import java.util.Map; import java.util.Set; 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, "Munish"); treeMap.put(3, "Pardeep");   //Print the TreeMap object. System.out.println("TreeMap elements:"); … Read more

Create treemap in java

Example package com.w3schools;   import java.util.Iterator; import java.util.Map; import java.util.Set; 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, "Munish"); treeMap.put(3, "Pardeep");   //Print the TreeMap object. System.out.println("TreeMap elements:"); … Read more

Eliminate duplicate user defined objects as a key from hashmap

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

Delete all elements from hashmap in java

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

Get entryset from hashmap in java

We can use entrySet() method to get entryset from hashmap 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.HashMap; import java.util.Map.Entry; import java.util.Set;   public class Test … Read more

Get list of keys from hashmap java

We can use keySet() method to get list of keys from hashmap 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.HashMap; import java.util.Set;   public class Test { public static void … Read more

Search a value in hashmap in java

We can use containsValue() method to search a value in hashmap 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.HashMap;   public class Test { public static void main(String args[]){ //Create HashMap object. HashMap<Integer, String> hashMap = new … Read more

Search a key in hashmap in java

We can use containsKey() method to search a key in hashmap 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.HashMap;   public class Test { public static void main(String args[]){ //Create HashMap object. HashMap<Integer, String> hashMap = new … Read more

Copy map content to another hashmap in java

We can use putAll() method to copy map content to another hashmap in java. putAll(Map m): Puts all the entries from m into this map. Syntax: public void putAll(Map m) Example package com.w3schools;   import java.util.HashMap;   public class Test { public static void main(String args[]){ //Create HashMap object. HashMap<Integer, String> hashMap = new HashMap<Integer, … Read more