How to Implement a Custom Order Sorting for Elements in a LinkedHashSet? Last Updated : 20 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like 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 JavaBelow 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); } } } OutputOriginal 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. Comment More infoAdvertise with us Next Article How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? J javvajisunil16 Follow Improve Article Tags : Java Java Programs java-LinkedHashSet Java Examples Practice Tags : Java Similar Reads How to Sort LinkedHashSet Elements in Descending Order in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet iteration is through the elements in 2 min read How to Implement a Custom Order or Sorting for Key-Value Pairs in a TreeMap in Java? The Java Collections Framework includes the TreeMap class, in which Java offers a sorted collection of key-value pairs. By default, TreeMap uses a custom Comparator or arranges components according to their natural ordering. In this article, we will learn how to apply a custom order for key-value pa 2 min read How to Create a LinkedHashSet with Custom Equality for Objects? In this article, we want to create a LinkedHashSet of custom objects with specific equality criteria. So, we can create it by overriding the equals and hashcode methods in Java. In the equals() method, we can put the custom criteria over there and we override the hashcode to provide the same Hashcod 3 min read How to Print LinkedHashSet Elements in Java? LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted. There are several ways to print LinkedHashSet elements: By simply printing the elementsBy using enhanced for loo 4 min read How to Sort LinkedHashSet Elements using Comparable Interface in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 3 min read Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde 4 min read Like