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...]
How to Sort HashMap in Java by Keys
We all know that HashMap will not save key-value pairs in any sort of order neither preserve the insertion order. In this tutorial we will learn how to sort a HashMap based on Keys. We will be using two approaches. TreeMap Collection class (Which has in-built support for sorting the elements using Comparable and Comparator Interface) Implementing the Comparator … [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...]
How HashMap works in Java
How a HashMap Works internally has become a popular question in almost all the interview. As almost everybody knows how to use a HashMap or the difference between HashMap and Hashtable. But many fails when the question is how does a hashmap internally works. So the answer to the question how does a hashmap works is that is it works based on the hashing principle but it is … [Read more...]
Difference between HashMap and Hashtable | HashMap Vs Hashtable
Both the HashMap and Hashtable implement the interface java.util.Map but there are some slight differences which has to be known to write a much efficient code. The Most important difference between HashMap and the Hashtable is that Hashtable is synchronized and HashMap is non-synchronized , which means Hashtable is thread-safe and can be shared among multiple threads and … [Read more...]