Sorting Hashtable contents

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

 

Solution:

  1. Ascending order: use TreeMap, by passing Hashtable entries as arguments to inter-conversion constructor
  2. Descending order: use TreeMap, by implementing Comparator interface and providing reverse sorting logic and finally putting all entries of Hashtable to TreeMap using putAll() method of Map interface

 

Example 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

 

 

Example 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

 

 

Important points:

  • Hashtable stores entries (key-value pairs) in random order
  • TreeMap stores entries (key-value pairs) in sorted order

 

 

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/7/docs/api/java/util/ArrayList.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/class-use/HashSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html
http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
https://docs.oracle.com/javase/7/docs/api/java/util/Hashtable.html

 

Happy Coding !!
Happy Learning !!

Various ways to iterate through Hashtable - 6 ways
Sorting HashMap by Keys and Values using stream in Java 8