This example shows how to sort Hashtable by values in Java. This example also shows how to sort Hashtable by values using the sort method of the Collections class and a custom Comparator.
How to sort the Hashtable by values in Java?
The Hashtable class in Java does not maintain the order of its entries, so even if we sort it, there is no guarantee that the elements will be returned in that order. We need to convert hashtable to a collection that maintains the element order.
How to sort Hashtable by values if you only want values?
We can get all the values from the hashtable using the values
method, convert it to a List (like ArrayList or LinkedList) and then we can sort the List using the sort
method of the Collections class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Hashtable<String, Integer> htRandomNumbers = new Hashtable<String, Integer>(); htRandomNumbers.put("Five", 5); htRandomNumbers.put("Three", 3); htRandomNumbers.put("Nine", 9); htRandomNumbers.put("Seven", 7); htRandomNumbers.put("Two", 2); System.out.println( "Hashtable contains: " + htRandomNumbers ); /* * Get all hashtable values using * the value method and convert it * to a List */ List<Integer> list = new ArrayList<Integer>( htRandomNumbers.values() ); /* * Sort the list using the sort method * of the Collections class */ Collections.sort(list); System.out.println("Sorted values: " + list); |
Output
1 2 |
Hashtable contains: {Seven=7, Three=3, Nine=9, Two=2, Five=5} Sorted values: [2, 3, 5, 7, 9] |
The sort
method of the Collections class sorts the list elements in their natural order, which is ascending order for the type Integer. If you want to sort the values in descending order, pass the reverse comparator in the sort
method as given below.
1 |
Collections.sort(list, Collections.reverseOrder()); |
Output
1 2 |
Hashtable contains: {Seven=7, Three=3, Nine=9, Two=2, Five=5} Sorted values: [9, 7, 5, 3, 2] |
How to sort Hashtable by values if you want key-value pairs?
In the above example, we sorted only the values of the Hashtable object. What if you want to get the Map that is sorted by the hashtable values?
In that case, we need to first get all entries or mappings from the hashtable using the entrySet
method and convert it to a List. Then we need to sort the list by entry value and put the sorted entries in a Map that maintains the insertion order e.g. LinkedHashMap as given below.
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 |
Hashtable<String, Integer> htRandomNumbers = new Hashtable<String, Integer>(); htRandomNumbers.put("Five", 5); htRandomNumbers.put("Three", 3); htRandomNumbers.put("Nine", 9); htRandomNumbers.put("Seven", 7); htRandomNumbers.put("Two", 2); System.out.println( "Hashtable contains: " + htRandomNumbers ); //get all the entries from the hashtable and put it in a List List<Map.Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(htRandomNumbers.entrySet()); //sort the entries based on the value by custom Comparator Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){ public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) { return entry1.getValue().compareTo( entry2.getValue() ); } }); Map<String, Integer> mapSortedByValues = new LinkedHashMap<String, Integer>(); //put all sorted entries in LinkedHashMap for( Map.Entry<String, Integer> entry : list ){ mapSortedByValues.put(entry.getKey(), entry.getValue()); } System.out.println("Map sorted by values: " + mapSortedByValues); |
Output
1 2 |
Hashtable contains: {Seven=7, Three=3, Nine=9, Two=2, Five=5} Map sorted by values: {Two=2, Three=3, Five=5, Seven=7, Nine=9} |
How to sort Hashtable of custom class objects by value?
If the hashtable values are objects of a custom class, then the custom class must implement the Comparable interface. The above examples worked because the Integer class has implemented the Comparable interface.
The hashtable value custom class needs to implement the Comparable interface and define the compareTo
method.
This example is a part of the Java Hashtable Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Hashtable Documentation