Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

A HashSet is a collection class from the java.util package. This class inherits from the AbstractSet class and implements the Set interface. Furthermore, a HashSet doesn't preserve the order of elements, hence the need to find ways to sort these elements.

In this quick tutorial, we'll learn multiple techniques to sort the elements of a HashSet.

2. Using the Collections.sort() Method

The Collections.sort() method sorts collections of objects that implement the java.util.List interface. Therefore, we can convert our HashSet into a List and then sort it using Collections.sort():

HashSet<Integer> numberHashSet = new HashSet<>();
numberHashSet.add(2);
numberHashSet.add(1);
numberHashSet.add(4);
numberHashSet.add(3);

// converting HashSet to arraylist
ArrayList arrayList = new ArrayList(numberHashSet);

// sorting the list
Collections.sort(arrayList);

assertThat(arrayList).containsExactly(1, 2, 3, 4);

In the example above, we first copied the elements of our HashSet into an ArrayList. Then, we used our ArrayList as an argument of the Collections.sort() method. Instead of ArrayList, we could also have used LinkedList or Vector.

3. Using a TreeSet

Using this approach, we convert the HashSet to a TreeSet, which is similar to the HashSet except that it stores the elements in ascending order. Therefore, the HashSet elements are put in order when the HashSet is converted to a TreeSet:

HashSet<Integer> numberHashSet = new HashSet<>();
numberHashSet.add(2);
numberHashSet.add(1);
numberHashSet.add(4);
numberHashSet.add(3);

TreeSet<Integer> treeSet = new TreeSet<>();
treeSet.addAll(numberHashSet);

assertThat(treeSet).containsExactly(1, 2, 3, 4);

We can see that using a TreeSet to sort a HashSet is very simple. We only need to create an instance of TreeSet with the HashSet list as an argument.

4. Using the stream().sorted() Method

There's a concise way to sort a HashSet using the stream().sorted() method of the Stream API. This API, introduced in Java 8, allows us to perform functional operations on a set of elements. Also, it can take objects from different collections and display them in the desired way, depending on the pipeline methods we use.

In our example, we'll use the stream().sorted() method, which returns a Stream whose elements are sorted in a certain order. It should be noted that since the original HashSet stays unmodified, we need to save the results of the sorting in a new Collection. We will use the collect() method to store the data back into a new HashSet:

HashSet<Integer> numberHashSet = new HashSet<>();
numberHashSet.add(200);
numberHashSet.add(100);
numberHashSet.add(400);
numberHashSet.add(300);

HashSet<Integer> sortedHashSet = numberHashSet.stream()
  .sorted()
  .collect(Collectors.toCollection(LinkedHashSet::new));

assertThat(sortedHashSet).containsExactly(100, 200, 300, 400);

We should note that when we use the stream().sorted() method without a parameter, it sorts the HashSet in the natural order. We can also overload it with a comparator to define a custom sort order.

5. Conclusion

In this article, we discussed how to sort a HashSet in Java using three ways: with the Collections.sort() method, using a TreeSet, and using the stream().sorted() method.

As always, the code snippets are available over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!