March 9, 2022

How to Sort Java HashSet

This post shows how to sort a HashSet in Java. Since HashSet is an unordered collection so you will need to convert it to another collection in order to sort a HashSet. There are two options to sort a HashSet by converting it to another collection.

  1. Convert HashSet to list and use Collections.sort() method where you can pass List as an argument.
  2. Convert HashSet to TreeSet by using constructor of the TreeSet class. Since TreeSet is a SortedSet implementation the elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

Sorting HashSet using Collections.sort() method

If you are sorting HashSet in Java using this way you need to convert HashSet to List and pass it to Collections.sort() method which will sort it as per natural ordering. If you want to sort it in different order you can use a Comparator.

Refer How to Sort ArrayList in Java to see sorting of the List by passing a Comparator.

Drawback of using this option is that it returns a list.

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

public class SortingHashSet {
  public static void main(String[] args) {
    Set<String> capitalSet = new HashSet<String>();
    // adding elements
    capitalSet.add("New Delhi");
    capitalSet.add("Lisbon");
    capitalSet.add("Buenos Aires");
    capitalSet.add("Beijing");
    capitalSet.add("Washington DC");
    System.out.println("Set Elements- " + capitalSet);
    //Convert HashSet to list
    List<String> capitalList = new ArrayList<>(capitalSet);
    // Sort ArrayList
    Collections.sort(capitalList);
    System.out.println("After Sorting " + capitalList);
  }
}
Output
Set Elements- [Beijing, Washington DC, New Delhi, Lisbon, Buenos Aires]
After Sorting [Beijing, Buenos Aires, Lisbon, New Delhi, Washington DC]

Sorting HashSet by converting to TreeSet

If you want to sort HashSet in Java using this way you just need to pass your HashSet to a constructor of TreeSet, it will be sorted in its natural order. By using this way to sort a HashSet, after sorting you'll still have a Set not a List as was the case with previous option.

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

public class SortingHashSet {
  public static void main(String[] args) {
    Set<String> capitalSet = new HashSet<String>();
    // adding elements
    capitalSet.add("New Delhi");
    capitalSet.add("Lisbon");
    capitalSet.add("Buenos Aires");
    capitalSet.add("Beijing");
    capitalSet.add("Washington DC");
    System.out.println("Set Elements- " + capitalSet);

    // To TreeSet
    Set<String> sortedCapitalSet = new TreeSet<String>(capitalSet);
    System.out.println("After Sorting " + sortedCapitalSet);
  }
}
Output
Set Elements- [Beijing, Washington DC, New Delhi, Lisbon, Buenos Aires]
After Sorting [Beijing, Buenos Aires, Lisbon, New Delhi, Washington DC]

That's all for the topic How to Sort Java HashSet. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment