First Approach is to using java 7:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { public static void main(String[] args) { //creating the map HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("t", 1642); map.put("h", 163); map.put("hwd", 613); map.put("hw", 183); // STEP1: creating a list of type map to store the map List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); // STEP2: calling sort api with a custom comparator. this comparator takes two objects of map and sort them based on values natural sorting order Collections.sort(list, new Comparator<Entry<String, Integer>>(){ @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o1.getValue().compareTo(o2.getValue()); } }); // STEP3: printing the sorted map for (Entry<String, Integer> entry : list) { System.out.println(entry.getKey()+"="+entry.getValue()); } } } |
Second Approach is to using Java8:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; public class Main { public static void main(String[] args) { //creating the map HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("t", 1642); map.put("h", 16343); map.put("hwd", 6133); map.put("hw", 183); // STEP1: creating a list of type map to store the map List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); // STEP2: calling sort api with a custom comparator. this comparator takes two objects of map and sort them based on values natural sorting order Collections.sort(list, (l1,l2)->l1.getValue().compareTo(l2.getValue())); // STEP3: printing the sorted map for (Entry<String, Integer> entry : list) { System.out.println(entry.getKey()+"="+entry.getValue()); } } } |
Q2. Sort a map based on Keys?
Answer: We can create a treemap that will sort the map based on keys.
PROGRAM:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.HashMap; public class Main { public static void main(String[] args) { //creating the TreeMap, by default it will sort the map based on keys HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("t", 1642); map.put("h", 16343); map.put("hwd", 6133); map.put("hw", 183); //printing the TreeMap map.forEach((k,v)->System.out.println(k+" "+v)); } } |
OUTPUT:
1 2 3 4 | t 1642 hwd 6133 h 16343 hw 183 |
Q3. How HashMap works internally?
This is a very frequently asked question in the java interview room. Basically, a hashmap creates an array of buckets. The default size is 16. In HashMap the load factor is 75% i.e. when 12 buckets are filled, it will double its size. Then it internally creates an index for the key using hashing and bitwise and operation. After storing an element in the node it inserts a second element. while inserting the second element if the same index is generated that means if a hash collision happens that it stores the second element to the next node of the linked list. And at the time of fetching it again uses the hashing algorithm to calculate the hashcode and index position. Then it matches the key using the equals method and returns the value if the key matches or move to the next node. Please see THIS detailed article for the full explanation.
Q4. Can we add duplicate keys in the hashmap if not what will be the error?
Yes. we can add duplicate keys in the hashmap. Neither Compile-time nor Run time error will be thrown. But the previously added value will be replaced by the new value associated with the key that we are adding in the hashMap.
Q5. Which operation is faster in hashmap?
HashMap is very efficient at checking if a key exists or retrieving a value based on a key. Those operations take O(1) on average. Adding and removing elements from a HashMap based on a key takes O(1) constant-time. Checking for an element without knowing the key takes linear time O(n), as it's necessary to loop over all the elements.
Q6. Difference between HashMap and HashTable?
![]() |
| Difference between HashMap and HashTable |
Q7. Difference between ArrayList and Vector?


