Java – 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

1. Sorting Hashtable contents:

  1. Ascending order: pass Hashtable entries as arguments to TreeSet class’ inter-conversion constructor
  2. 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:

References:

Happy Coding !!
Happy Learning !!

Java - Iterate through Hashtable in 6 ways
Java 8 - Sorting HashMap by Keys and Values using Stream