Java – How to Sort HashSet in 2 ways ?

In this article, we will discuss how to sort HashSet in 2 ways. Already, in one of the earlier article we discussed about HashSet sorting in Ascending & Descending order

As it is very well known that, HashSet stores elements in random-order. Therefore, to sort HashSet contents, we need to convert it to some other Collection class like ArrayList or TreeSet as,

  • ArrayList maintains insertion-order after sorting
  • TreeSet stores element/objects in sorted order

Sorting HashSet – 2 ways :

  1. Using Collections.sort(list); method
  2. Using TreeSet inter-conversion constructor

Let us move forward to discuss 2 ways of sorting HashSet contents

1. Using Collections.sort(list) method :

  1. Create new HashSet object
  2. Store HashSet contents into ArrayList using inter-conversion constructor
  3. Finally, invoke Collections.sort(al); method to sort elements in ascending order
  4. Note: similarly elements can be sorted in descending order as well using Comparator

Syntax:

List<String> al = new ArrayList<String>(hsObject);

Collections.sort(al);

SortingHashSetUsingCollections.java

package in.bench.resources.java.sorting.hashset;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SortingHashSetUsingCollections {

	public static void main(String[] args) {

		// create HashSet of String type
		Set<String> coaches = new HashSet<String>();

		// add string-values to HashSet
		coaches.add("Kirsten");
		coaches.add("Anil");
		coaches.add("John");
		coaches.add("Fletcher");
		coaches.add("Madan");
		coaches.add("Bishen");
		coaches.add("Chappell");

		// Before Sorting
		System.out.println("Before Sorting :"
				+ " HashSet contents in random order\n");
		for(String coach : coaches) {
			System.out.println(coach);
		}

		// create ArrayList and store HashSet contents
		List<String> alCoaches = new ArrayList<String>(coaches);

		// sort using Collections.sort(); method
		Collections.sort(alCoaches);

		// After Sorting
		System.out.println("\n\nAfter Sorting : Sorted order\n");
		for(String coach : alCoaches) {
			System.out.println(coach);
		}
	}
}

Output:

Before Sorting : HashSet contents in random order

Kirsten
Chappell
Madan
John
Bishen
Fletcher
Anil


After Sorting : Sorted order

Anil
Bishen
Chappell
Fletcher
John
Kirsten
Madan

2. Using TreeSet inter-conversion constructor :

  1. Create new HashSet object
  2. Create TreeSet object and pass HashSet contents as constructor-argument to TreeSet
  3. That’s it, HashSet are sorted and stored inside new TreeSet object
  4. Note: similarly elements can be sorted in descending order as well using Comparator

Syntax:

TreeSet<String> set = new TreeSet<String>(hashSet);

SortingHashSetUsingTreeSet.java

package in.bench.resources.java.sorting.hashset;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class SortingHashSetUsingTreeSet {

	public static void main(String[] args) {

		// create HashSet of String type
		Set<String> coaches = new HashSet<String>();

		// add string values to HashSet
		coaches.add("Kirsten");
		coaches.add("Anil");
		coaches.add("John");
		coaches.add("Fletcher");
		coaches.add("Madan");
		coaches.add("Bishen");
		coaches.add("Chappell");

		// Before Sorting
		System.out.println("Before Sorting :"
				+ " HashSet contents in random order\n");
		for(String coach : coaches) {
			System.out.println(coach);
		}

		// ASC Sorting : create TreeSet and store HashSet contents
		// using inter-conversion constructor
		Set<String> alCoaches = new TreeSet<String>(coaches);

		// After Sorting
		System.out.println("\n\nAfter Sorting :\n");
		for(String coach : alCoaches) {
			System.out.println(coach);
		}
	}
}

Output:

Before Sorting : HashSet contents in random order

Kirsten
Chappell
Madan
John
Bishen
Fletcher
Anil


After Sorting :

Anil
Bishen
Chappell
Fletcher
John
Kirsten
Madan

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - byte[] Arrays Sorting with example
Java 8 - Conversion of List to Map