Sort LinkedHashSet in Java Example

This example shows how to sort LinkedHashSet in Java. This example also shows how to sort LinkedHashSet in ascending or descending order using TreeSet, Comparable, and Comparator.

The LinkedHashSet class in Java is a hash table and linked list implementation of the Set interface. It guarantees the insertion order of its elements and does not allow duplicate elements.

How to Sort LinkedHashSet in Java?

There are a couple of ways using which we can sort the linked hash set object.

1. Using the TreeSet class

We can convert the LinkedHashSet to a TreeSet using the TreeSet constructor to automatically sort the elements of the linked hash set object.

Output

How to sort LinkedHashSet elements in descending order?

To sort the elements in descending order, we will use the reverseOrder method of the Collections class.

The reverseOrder method returns a Comparator that imposes the reverse of the natural ordering on collection objects. We will create a new TreeSet object with the reverse Comparator and then add all elements of the LinkedHashSet object to the TreeSet object.

Output

How to sort LinkedHashSet of custom class objects using TreeSet?

If the LinkedHashSet contains elements that are objects of a custom class, then the custom class must implement the Comparable interface or a custom comparator object must be provided in the TreeSet constructor.

If none of these is done, the code throws java.lang.ClassCastException: cannot be cast to java.lang.Comparable as given below.

Output

We need to implement the Comparable interface in the User class or provide a Comparator object in the TreeSet constructor. I am going to show how to implement the Comparable interface in the below given example.

Output

To sort the LinkedHashSet of custom class objects in the descending order, we need to change the line having the TreeSet constructor with the below given code.

Output

2. Using the sort method of the Collections class

We can also sort the LinkedHashSet elements using the sort method of the Collections class, but before we can use that we need to convert the LinkedHashSet object to a List object like an ArrayList or a LinkedList.

Output

For the LinkedList having elements of a custom class, the custom class must implement the Comparable interface or a custom comparator must be provided in the overloaded sort method.

Since we have already implemented the Comparable interface in our User class, the sort method works fine for the List of User objects as given below.

Output

For sorting the custom class objects in descending order, we need to provide the reverse comparator in the sort method as given below.

Output

This example is a part of the LinkedHashSet in Java Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 LinkedHashSet

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping