In my previous post we have learnt How to Sort HashMap in Java by Keys, in this article we will learn to sort HashMap values. We will be using the below three approaches. Implementing the Comparator Interface along with TreeMap Collection Implementing a separate class implementing Comparator Interface Using Collections.sort() method … [Read more...]
Java HashMap putAll(Map m) Example
The put(K key, V value) method of java.util.HashMap class associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced whereas putAll(Map<? extends K,? extends V> m) method copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this … [Read more...]
Java HashMap entrySet() Example
The entrySet() method of java.util.HashMap class returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. … [Read more...]
Java HashMap containsValue(Object value) Example
The containsKey(Object key) method of java.util.HashMap class returns true if this map contains a mapping for the specified key where as containsValue(Object value) method returns true if this map maps one or more keys to the specified value. … [Read more...]