In this article, we will discuss how to sort HashSet. 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:
- Using Collections.sort(list); method
- Using TreeSet inter-conversion constructor
Let us move forward to discuss 2 ways of sorting HashSet contents
Way 1: Using Collections.sort(list); method
Steps:
- Create new HashSet object
- Store HashSet contents into ArrayList using inter-conversion constructor
- Finally, invoke Collections.sort(al); method to sort elements in ascending order
- 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
Way 2: Using TreeSet inter-conversion constructor
Steps:
- Create new HashSet object
- Create TreeSet object and pass HashSet contents as constructor-argument to TreeSet
- That’s it, HashSet are sorted and stored inside new TreeSet object
- 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.
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/7/docs/api/java/util/Collections.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
Happy Coding !!
Happy Learning !!