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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.LinkedHashSet; import java.util.TreeSet; public class SortLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); /* * To sort LinkedHashSet, convert it to * a TreeSet using the constructor */ TreeSet<Integer> tSet = new TreeSet<Integer>( lhSetNumbers ); System.out.println("TreeSet contains: " + tSet); } } |
Output
1 |
TreeSet contains: [21, 22, 23, 24, 25, 26] |
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.
1 |
public static <T> Comparator<T> reverseOrder() |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); /* * To sort the LinkedHashSet in descending order, * use the reverseOrder method of the Collections class. */ TreeSet<Integer> tSet = new TreeSet<Integer>( Collections.reverseOrder() ); //add all elements of the LinkedHashSet tSet.addAll(lhSetNumbers); System.out.println("TreeSet contains: " + tSet); |
Output
1 |
TreeSet contains: [26, 25, 24, 23, 22, 21] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.LinkedHashSet; import java.util.TreeSet; class User{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } } public class SortLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<User> lhSetUsers = new LinkedHashSet<User>(); lhSetUsers.add( new User(3) ); lhSetUsers.add( new User(1) ); lhSetUsers.add( new User(2) ); TreeSet<User> tSet = new TreeSet<User>( lhSetUsers ); } } |
Output
1 2 3 4 5 6 7 |
Exception in thread "main" java.lang.ClassCastException: com.javacodeexamples.collections.linkedhashset.User cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.TreeSet.add(Unknown Source) at java.util.AbstractCollection.addAll(Unknown Source) at java.util.TreeSet.addAll(Unknown Source) at java.util.TreeSet.<init>(Unknown Source) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.util.LinkedHashSet; import java.util.TreeSet; //step 1: Implement the Comparable interface class User implements Comparable<User>{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } //Step 2: define the compareTo method public int compareTo(User otherUser) { return this.getId() - otherUser.getId(); } } public class SortLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<User> lhSetUsers = new LinkedHashSet<User>(); lhSetUsers.add( new User(3) ); lhSetUsers.add( new User(1) ); lhSetUsers.add( new User(2) ); TreeSet<User> tSet = new TreeSet<User>( lhSetUsers ); System.out.println("TreeSet contains: " + tSet); } } |
Output
1 |
TreeSet contains: [User -> 1, User -> 2, User -> 3] |
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.
1 2 3 4 |
TreeSet<User> tSet = new TreeSet<User>( Collections.reverseOrder() ); tSet.addAll(lhSetUsers); System.out.println("TreeSet contains: " + tSet); |
Output
1 |
TreeSet contains: [User -> 3, User -> 2, User -> 1] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
LinkedHashSet<Integer> lhSetNumbers = new LinkedHashSet<Integer>(); lhSetNumbers.add(26); lhSetNumbers.add(23); lhSetNumbers.add(24); lhSetNumbers.add(21); lhSetNumbers.add(25); lhSetNumbers.add(22); //convert LinkedHashSet to a List List<Integer> listNumbers = new ArrayList<Integer>( lhSetNumbers ); Collections.sort(listNumbers); System.out.println("Ascending order List contains: " + listNumbers); /* * To sort in descending order, use the * overloaded sort method and specify the * reverse order comparator */ Collections.sort(listNumbers, Collections.reverseOrder()); System.out.println("Descending order List contains: " + listNumbers); |
Output
1 2 |
Ascending order List contains: [21, 22, 23, 24, 25, 26] Descending order List contains: [26, 25, 24, 23, 22, 21] |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; //step 1: Implement the Comparable interface class User implements Comparable<User>{ private int id; public User(int id){ this.id = id; } public int getId(){ return this.id; } public String toString(){ return "User -> " + getId(); } //Step 2: define the compareTo method public int compareTo(User otherUser) { return this.getId() - otherUser.getId(); } } public class SortLinkedHashSetExample { public static void main(String[] args) { LinkedHashSet<User> lhSetUsers = new LinkedHashSet<User>(); lhSetUsers.add( new User(3) ); lhSetUsers.add( new User(1) ); lhSetUsers.add( new User(2) ); //convert LinkedHashSet to a List List<User> listUsers = new ArrayList<User>( lhSetUsers ); Collections.sort(listUsers); System.out.println(listUsers); } } |
Output
1 |
[User -> 1, User -> 2, User -> 3] |
For sorting the custom class objects in descending order, we need to provide the reverse comparator in the sort
method as given below.
1 2 |
Collections.sort(listUsers, Collections.reverseOrder()); System.out.println(listUsers); |
Output
1 |
[User -> 3, User -> 2, User -> 1] |
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