How to Sort HashMap in Java by Values

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.

  1. Implementing the Comparator Interface along with TreeMap Collection
  2. Implementing a separate class implementing Comparator Interface
  3. 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 map had for any of the keys currently in the specified map.
[Read more…]