Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

How to Sort LinkedHashSet in Java?

  • Last Updated : 04 Jan, 2021

The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

There are three ways to sort LinkedHashSet in Java. Which is the following:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Method 1(Using ArrayList)

In this method, we convert the LinkedHashSet into an ArrayList and then we sort the ArrayList using the sort() method of collections class.



// Convert LinkedHashSet to an ArrayList
ArrayList<Integer> array = new ArrayList<>(set);

// sort ArrayList
Collections.sort(array);

Declaration

public static void sort(List myList)

Parameters: myList is an object we want to sort. 

Example:

Java




// Java program to demonstrate how to sort LinkedHashSet
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // New Empty LinkedHashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Adding elements
        set.add(10);
        set.add(50);
        set.add(20);
        set.add(40);
        set.add(30);
  
        // Print LinkedHashSet before sort
        System.out.println("Before sort: " + set);
  
        // Convert LinkedHashSet to an ArrayList
        ArrayList<Integer> array = new ArrayList<>(set);
  
        // sort ArrayList
        Collections.sort(array);
  
        // Print after sort
        System.out.println("After sort: " + array);
    }
}
Output
Before sort: [10, 50, 20, 40, 30]
After sort: [10, 20, 30, 40, 50]

Method 2: Using TreeSet

In this method, we convert LinkedHashSet to a TreeSet using the constructor to automatically sort the elements.

// Convert LinkedHashSet to a TreeSet
TreeSet<Integer> tree_set = new TreeSet<>(set);

Example:

Java




// Java program to demonstrate how to sort LinkedHashSet
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // New Empty LinkedHashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Adding elements
        set.add(10);
        set.add(50);
        set.add(20);
        set.add(40);
        set.add(30);
  
        // Print LinkedHashSet before sort
        System.out.println("Before sort: " + set);
  
        // Convert LinkedHashSet to a TreeSet using
        // constructor
        TreeSet<Integer> tree_set = new TreeSet<>(set);
  
        // Print after sort
        System.out.println("After sort: " + tree_set);
    }
}
Output
Before sort: [10, 50, 20, 40, 30]
After sort: [10, 20, 30, 40, 50]

Method 3: Using stream

In this method, we sort the LinkedHashSet using stream and sorted() function of the stream.

// Sort and print using stream
set.stream().sorted().forEach(System.out::println);

Below is the implementation:

Example:

Java




// Java program to demonstrate how to sort LinkedHashSet
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        // New Empty LinkedHashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Adding elements
        set.add(10);
        set.add(50);
        set.add(20);
        set.add(40);
        set.add(30);
  
        // Print LinkedHashSet before sort
        System.out.println("Before sort: " + set);
  
        // Print after sort
        System.out.println("After sort: ");
  
        // Sort and print using stream
        set.stream().sorted().forEach(System.out::println);
    }
}
Output
Before sort: [10, 50, 20, 40, 30]
After sort: 
10
20
30
40
50



My Personal Notes arrow_drop_up
Recommended Articles
Page :