How to Sort LinkedHashSet Elements in Descending Order in Java? Last Updated : 17 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned to the order in which they were inserted. Elements in HashSet does maintain order while TreeSet maintains objects in sorted order defined by either comparable or comparator method in Java. TreeSet elements are sorted in ascending order by default. So now the problem that occurs is to sort given HashSet in descending order. So here with the help of a TreeSet is necessary to store the element in descending order. Illustration: Input : LinkedHashSet = [4, 3, 6, 5, 8] Output: LinkedHashSet = [8, 6, 5, 4, 3] Input: LinkedHashSet = [22, 44, 33, 66, 55] Output: LinkedHashSet = [66, 55, 44, 33, 22] Algorithm: Create HashSet to take input and store all the elements from the user.Now, create TreeSet which stores the elements in decreasing order by adding all the elements from above HashSet in reverse order.Pseudo Code: TreeSet<Integer> ts = new TreeSet<>(Collections.reverseOrder()); ts.addAll(lh); Example: Java // Java Program to sort LinkedHashSet elements // in descending order // Importing java generic libraries import java.util.*; import java.io.*; // Class public class GFG { // Main driver method public static void main(String[] args) { // Creating and Initializing LinkedHashSet Set<Integer> linkhasset = new LinkedHashSet<Integer>(); // Adding elements to above LinkedHashSet // Custom inputs linkhasset.add(26); linkhasset.add(23); linkhasset.add(24); linkhasset.add(21); linkhasset.add(25); linkhasset.add(22); // TreeSet storing elements in descending order by // adding all elements of HashSet in reverse order TreeSet<Integer> ts = new TreeSet<>(Collections.reverseOrder()); // Add all elements from LinkedHashSet to TreeSet ts.addAll(linkhasset); // Print all elements of TreeSet System.out.println("Element in descending order : " + ts); } } OutputElement in descending order : [26, 25, 24, 23, 22, 21] Comment More infoAdvertise with us Next Article How to Find the Element Index in LinkedHashSet in Java? P patelnagendra1 Follow Improve Article Tags : Java Java Programs java-LinkedHashSet Practice Tags : Java Similar Reads 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 How to Implement a Custom Order Sorting for Elements in a LinkedHashSet? 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 2 min read How to sort TreeSet in descending order in Java? Given a TreeSet in Java, task is to sort elements of TreeSet in Descending Order (descreasing order).Examples: Input : Set: [2, 3, 5, 7, 10, 20] Output : Set: [20, 10, 7, 5, 3, 2] Input : Set: [computer, for, geeks, hello] Output : Set: [hello, geeks, for, computer] Approach: To make a TreeSet Eleme 1 min read How to Find the Element Index in LinkedHashSet in Java? LinkedHashSet is used to store distinct items and get the items in which order they were inserted in Java. LinkedHashSet does not store values based on the index. But there are some methods to find the element index in LinkedHashSet in Java. Method 1: (By converting LinkedHashSet to ArrayList) To fi 4 min read How to Get Random Elements from LinkedHashSet in Java? LinkedHashSet is used to maintain the insertion order and for generating random elements from LinkedHashSet we will use Random Class to generate a random number between 0 and the LinkedHashSet size. That random number will act as the index of LinkedHashSet. We can get a random element in three ways: 3 min read Like