There are multiple ways to compare objects in Java including the equals, compare and compareTo methods. Collection classes like HashMap store objects using the hashcode and there is no guaranteed order in which elements are stored in the collection class. But there are ways in which the elements can be sorted as per the requirement.

  • Converting HashMap to TreeMap : The most easiest way is to convert the HashMap to TreeMap for sorting. This is convenient because TreeMap has in-built support for sorting the elements by the use of Comparable and Comparator interfaces. The following code demonstrates how the conversion of HashMap to TreeMap can be done:
    new TreeMap(hashMap); 
    

    As you can see, TreeMap offers a constructor which accepts another map as argument. The javadoc for this constructor states that:

    Constructs a new tree map containing the same mappings as the given
    map, ordered according to the natural ordering of its keys

    As stated by javadoc, the key objects should be mutually comparable by using the compareTo method.

  • Other way to sort the keys of a HashMap is to use the Set and Collections.sort(ArrayList) as shown below:
    HashMap hm = new HashMap();
    hm.put("a", new Integer(1));
    hm.put("b", new Integer(2));
    hm.put("c", new Integer(3));
    
    Set s=hm.keySet();
    String[] keys=new String[s.size()];
    s.toArray(keys);
    List keyList = Arrays.asList(keys);
    
    Collections.sort(keyList);
    
    for(String key:keyList){
    System.out.println(key+":"+hm.get(key));
    

    In the above code, we are first getting a Set of keys and then converting this set of keys to ArrayList. There is a convenient method named sort in Collections class which can be used for sorting of arraylist elements. After sorting the keys we, get the value object for each individual key which gives us value of keys in sorted order.

  • For sorting the values of HashMap, we need to get the value objects instead of key objects. After getting the value objects, we can sort them using ArrayList as shown in above example for sorting by keys. Once sorting of value objects has been done, we can easily display the key and associated value. For implementing this method, the values should be unique and also we need to make use of the Entry class. An Entry represents a key value pair for HashMap.
  • If you are looking to store objects in the insertion order, then Java provides LinkedHashMap class. The property of LinkedHashMap is that the elements of this HashMap are backed by a linked list. You use the LinkedHashMap in similar way as a HashMap without any need to worry about how and where elements are getting stored.
  • The Apache Commons Library also offers a number of ways in which the HashMap can be sorted. Some of the classes which can be used for sorting from Apache Commons Collections library are IterableMap and OrderedMap. A complete guide to Apache Commons collections library is available on apache commons website
  • .

5 ways to sort HashMap in Java based on keys or values admin Core Java
There are multiple ways to compare objects in Java including the equals, compare and compareTo methods. Collection classes like HashMap store objects using the hashcode and there is no guaranteed order in which elements are stored in the collection class. But there are ways in which the elements can...
There are multiple ways to compare objects in Java including the equals, <a href="http://www.javaexperience.com/compare-objects-using-comparator-in-java/">compare</a> and <a href="http://www.javaexperience.com/overriding-compareto-method-in-java/">compareTo</a> methods. Collection classes like HashMap store objects using the hashcode and there is no guaranteed order in which elements are stored in the collection class. But there are ways in which the elements can be sorted as per the requirement. <ul> <li><strong>Converting HashMap to TreeMap :</strong> The most easiest way is to convert the HashMap to TreeMap for sorting. This is convenient because TreeMap has in-built support for sorting the elements by the use of <a href="http://www.javaexperience.com/difference-between-comparable-and-comparator-interfaces/">Comparable and Comparator interfaces</a>. The following code demonstrates how the conversion of HashMap to TreeMap can be done: 1 As you can see, TreeMap offers a constructor which accepts another map as argument. The javadoc for this constructor states that: <blockquote> Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys </blockquote> As stated by javadoc, the key objects should be mutually comparable by using the compareTo method.</li> <li><strong>Other way to sort the keys of a HashMap is to use the Set and Collections.sort(ArrayList)</strong> as shown below: 1 In the above code, we are first getting a Set of keys and then converting this set of keys to ArrayList. There is a convenient method named sort in Collections class which can be used for sorting of arraylist elements. After sorting the keys we, get the value object for each individual key which gives us value of keys in sorted order.</li> <li>For sorting the values of HashMap, we need to get the value objects instead of key objects. After getting the value objects, we can sort them using ArrayList as shown in above example for sorting by keys. Once sorting of value objects has been done, we can easily display the key and associated value. For implementing this method, the values should be unique and also we need to make use of the <strong>Entry class</strong>. An Entry represents a key value pair for HashMap.</li> <li>If you are looking to store objects in the insertion order, then Java provides LinkedHashMap class. The property of LinkedHashMap is that the elements of this HashMap are backed by a linked list. You use the LinkedHashMap in similar way as a HashMap without any need to worry about how and where elements are getting stored. </li> <li>The Apache Commons Library also offers a number of ways in which the HashMap can be sorted. Some of the classes which can be used for sorting from Apache Commons Collections library are <strong>IterableMap and OrderedMap</strong>. A complete guide to Apache Commons collections library is available on <a href="http://commons.apache.org/collections/">apache commons website</a></li> .</ul>