This example shows how to sort hashtable by keys in Java. This example also shows how to sort Hashtable by keys using the TreeMap class.
How to sort Hashtable by keys?
The Hashtable keys can be sorted using the TreeMap. The TreeMap class in Java automatically sorts the entries using the natural ordering of the keys or by the custom comparator provided in the constructor.
We can use the TreeMap constructor to convert the hashtable to a treemap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Hashtable; import java.util.TreeMap; public class SortHashtableByKeysExample { public static void main(String[] args) { Hashtable<Integer, String> hashtable = new Hashtable<Integer, String>(); hashtable.put(1, "One"); hashtable.put(3, "Three"); hashtable.put(2, "Two"); System.out.println("Hashtable contains: " + hashtable); /* * To sort hashtable by keys, convert it * to a TreeMap using the constructor */ TreeMap<Integer, String> tmap = new TreeMap<Integer, String>( hashtable ); System.out.println("TreeMap contains: " + tmap); } } |
Output
1 2 |
Hashtable contains: {3=Three, 2=Two, 1=One} TreeMap contains: {1=One, 2=Two, 3=Three} |
How to sort Hashtable of custom class objects by keys?
Let’s try to convert a Hashtable of custom class objects to a treemap object.
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 |
import java.util.Hashtable; import java.util.TreeMap; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "[User: " + getId() + "]"; } } public class SortHashtableByKeysExample { public static void main(String[] args) { Hashtable<User, Integer> htUsers = new Hashtable<User, Integer>(); htUsers.put(new User(105), 105); htUsers.put(new User(103), 103); htUsers.put(new User(102), 102); htUsers.put(new User(101), 101); htUsers.put(new User(104), 104); System.out.println("Hashtable contains: " + htUsers); TreeMap<User, Integer> treemap = new TreeMap<User, Integer>( htUsers ); System.out.println("TreeMap contains: " + treemap); } } |
Output
1 2 3 4 5 6 7 |
Hashtable contains: {[User: 105]=105, [User: 104]=104, [User: 101]=101, [User: 102]=102, [User: 103]=103} Exception in thread "main" java.lang.ClassCastException: com.javacodeexamples.collections.hashtable.User cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.AbstractMap.putAll(Unknown Source) at java.util.TreeMap.putAll(Unknown Source) at java.util.TreeMap.<init>(Unknown Source) |
If the key of the TreeMap is an object of a custom class, then the custom class either must implement the Comparable interface or a custom Comparator must be provided in the TreeMap constructor. If none of that is done, the code throws java.lang.ClassCastException: cannot be cast to java.lang.Comparable exception as we got.
Let’s implement the Comparable interface in the User class and try again.
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 41 42 43 44 45 46 47 48 49 |
import java.util.Hashtable; import java.util.TreeMap; /* * Step 1: Implement the Comparable interface */ class User implements Comparable<User>{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "[User: " + getId() + "]"; } /* * Define the compareTo method */ public int compareTo(User otherUser) { return this.getId() - otherUser.getId(); } } public class SortHashtableByKeysExample { public static void main(String[] args) { Hashtable<User, Integer> htUsers = new Hashtable<User, Integer>(); htUsers.put(new User(105), 105); htUsers.put(new User(103), 103); htUsers.put(new User(102), 102); htUsers.put(new User(101), 101); htUsers.put(new User(104), 104); System.out.println("Hashtable contains: " + htUsers); TreeMap<User, Integer> treemap = new TreeMap<User, Integer>( htUsers ); System.out.println("TreeMap contains: " + treemap); } } |
Output
1 2 |
Hashtable contains: {[User: 105]=105, [User: 104]=104, [User: 101]=101, [User: 102]=102, [User: 103]=103} TreeMap contains: {[User: 101]=101, [User: 102]=102, [User: 103]=103, [User: 104]=104, [User: 105]=105} |
As we can see from the output, now the TreeMap is sorted by the hashtable custom class keys.
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