In this article, we will discuss how to sort Hashtable contents
We can sort Hashtable in both ascending and descending order, by passing Hashtable contents as argument to TreeMap’s inter-conversion constructor
1. Sorting Hashtable contents:
- Ascending order: pass Hashtable entries as arguments to TreeSet class’ inter-conversion constructor
- Descending order: implement Comparator interface by providing reverse sorting logic and finally putting all entries of Hashtable to TreeMap class’ using putAll(); method of Map interface
1.1 Converting Hashtable to Sorted TreeMap in Ascending-order
Syntax:
TreeMap<String> set = new TreeMap<String>(hashtable);
SortingHashtableInAscendingOrder.java
package in.bench.resources.java.map;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class SortingHashtableInAscendingOrder {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> hashtable =
new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
hashtable.put("Microsoft", "Bill Gates");
hashtable.put("Apple", "Steve Jobs");
hashtable.put("Google", "Sundar Pichai");
hashtable.put("LinkedIn", "Reid Hoffman");
hashtable.put("Facebook", "Mark Zuckerberg");
// Random Order iterating
System.out.println("Before Sorting : Random Order \n");
// getting keySet() into Set
Set<String> set = hashtable.keySet();
// get Iterator from key set
Iterator<String> itr = set.iterator();
// iterating in random order
while(itr.hasNext()) {
String key = itr.next();
System.out.println("Key : " + key + "\t\t"
+ "Value : " + hashtable.get(key));
}
// After Sorting : Ascending order
System.out.println("\n\n\nAfter Sorting : Ascending order\n");
// convert to TreeMap
Map<String, String> ts = new TreeMap<String, String>(hashtable);
// iterate acc to ascending order of keys
for(String strKey : ts.keySet()){
System.out.println("Key : " + strKey + "\t\t"
+ "Value : " + hashtable.get(strKey));
}
}
}
Output:
Before Sorting : Random Order
Key : LinkedIn Value : Reid Hoffman
Key : Facebook Value : Mark Zuckerberg
Key : Google Value : Sundar Pichai
Key : Apple Value : Steve Jobs
Key : Microsoft Value : Bill Gates
After Sorting : Ascending order
Key : Apple Value : Steve Jobs
Key : Facebook Value : Mark Zuckerberg
Key : Google Value : Sundar Pichai
Key : LinkedIn Value : Reid Hoffman
Key : Microsoft Value : Bill Gates
1.2 Converting Hashtable to Sorted TreeMap in Descending order
Syntax:
TreeMap<String> map = new TreeMap<String>(reverseCompLogic);
map.putAll(hashtable);
SortingHashtableInDescendingOrder.java
package in.bench.resources.java.map;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class SortingHashtableInDescendingOrder {
public static void main(String[] args) {
// creating Hashtable object of type <String, String>
Hashtable<String, String> hashtable =
new Hashtable<String, String>();
// adding key-value pairs to Hashtable object
hashtable.put("Google", "Sundar Pichai");
hashtable.put("Facebook", "Mark Zuckerberg");
hashtable.put("LinkedIn", "Reid Hoffman");
hashtable.put("Apple", "Steve Jobs");
hashtable.put("Microsoft", "Bill Gates");
// Random Order iterating
System.out.println("Before Sorting : Random Order \n");
// getting keySet() into Set
Set<String> set = hashtable.keySet();
// get Iterator from key set
Iterator<String> itr = set.iterator();
// iterating in random order
while(itr.hasNext()) {
String key = itr.next();
System.out.println("Key : " + key + "\t\t"
+ "Value : " + hashtable.get(key));
}
// After Sorting : Ascending order
System.out.println("\n\n\nAfter Sorting : Descending order\n");
// convert to TreeMap
Map<String, String> ts =
new TreeMap<String, String>(Collections.reverseOrder());
// put all key-value into TreeMap
ts.putAll(hashtable);
// iterate acc to descending order of keys
for(String strKey : ts.keySet()){
System.out.println("Key : " + strKey + "\t\t"
+ "Value : " + hashtable.get(strKey));
}
}
}
Output:
Before Sorting : Random Order
Key : LinkedIn Value : Reid Hoffman
Key : Facebook Value : Mark Zuckerberg
Key : Microsoft Value : Bill Gates
Key : Apple Value : Steve Jobs
Key : Google Value : Sundar Pichai
After Sorting : Descending order
Key : Microsoft Value : Bill Gates
Key : LinkedIn Value : Reid Hoffman
Key : Google Value : Sundar Pichai
Key : Facebook Value : Mark Zuckerberg
Key : Apple Value : Steve Jobs
2. Important points:
- Hashtable stores entries or key-value pairs in random-order
- TreeMap stores entries or key-value pairs in sorting-order
Related Articles:
- Java – Sorting HashMap by Keys and Values
- Java – Converting Unordered HashMap into Sorted TreeMap
- Java – How to sort LinkedHashMap by its Keys ?
- Java – How to sort LinkedHashMap by its Values ?
- Java – How to Sort TreeMap by it Keys in descending-order ?
- Java – Sorting Hashtable contents
- Java 8 – Sorting HashMap by Keys and Values using Stream
- Java 8 – How to Sort HashMap entries by its Keys – 6 ways ?
- Java 8 – How to Sort HashMap entries by its Values – 6 ways ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html
Happy Coding !!
Happy Learning !!