Open In App

How to Implement a Custom Order Sorting for Elements in a LinkedHashSet?

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, LinkedHashSet is the implementation is different from the HashSet. HashSet maintains the Doubly-LinkedList across all of its elements of the LinkedHashSet. In LinkedHashSet, it maintains to sort all elements of the object in a List.

In this article, we will learn to implement a custom order or sorting for elements in a LinkedHashSet in Java.

Implementation Process:

  • The LinkedHashSet contains unique elements, and it does not directly sort all elements.
  • So, we can convert the LinkedHashSet to a List.
  • In this List, we can iterate the insertion of sorted elements by using comparator and comparable interface classes.

Syntax to Convert LinkedHashSet to List:

List<String> list = new ArrayList<>( )

Syntax to sort the List using a custom Comparator:

list.sort(new Comparator<String>() {
@Override
public int compare(String str1, Strig str2) {
return Integer.compare(str1.length(), str2.length());
}
});

Syntax to Convert List back to LinkedHashSet:

LinkedHashSet<String> sortedFruits = new LinkedHashSet<>(list);

Program to Implement a Custom Order or Sorting for elements in a LinkedHashSet in Java

Below are the implementations to Implement a Custom order or Sorting for Elements in a LinkedHashSet.

Approach: By Converting LinkedHashSet to List and then using Sorting

Java




// Java program to implement a custom order or sorting for elements in a LinkedHashSet
// by converting LinkedHashSet to a List and then using sorting
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
  
public class CustomSortingLinkedHashSet 
{
    public static void main(String[] args) 
    {
          LinkedHashSet<String> fruits = new LinkedHashSet<String>();
  
        fruits.add("Kiwi");
        fruits.add("Strawberry");
        fruits.add("Mango");
        fruits.add("Plum");
  
        System.out.println("Original order:" );
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
  
        // convert LinkedHashSet to a List
        List<String> list = new ArrayList<>(fruits);
  
        // sort the List based on custom criteria (length of strings)
        list.sort(new Comparator<String>() {
            
            @Override
            public int compare(String str1, String str2) 
            {
                // compare the length of strings
                return Integer.compare(str1.length(), str2.length());
            }
        });
  
        // convert the sorted List back to a LinkedHashSet
        LinkedHashSet<String> sortedFruits = new LinkedHashSet<>(list);
  
        System.out.println("Custom sorted order:");
        for (String fruit : sortedFruits) {
            System.out.println(fruit);
        }
    }
}


Output

Original order:
Kiwi
Strawberry
Mango
Plum
Custom sorted order:
Kiwi
Plum
Mango
Strawberry



Explanation of the above Program:

  • We have created a LinkedHashSet objects and its type of String.
  • After that we have added all the elements in LinkedHashSet objects of the String.
  • Next, we convert the LinkedHashSet to List of the String.
  • This List is sorting all elements by using custom Comparator interface.
  • After that we convert List to LinkedHashSet of all String elements in sorted custom order.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads