Java – How to sort LinkedHashSet contents ?

In this article, we will discuss how to sort contents of LinkedHashSet

Key points about LinkedList:

  • LinkedHashSet maintains insertion-order
  • It uses doubly-linked list and hash table to store element/objects

Different ways to sort LinkedHashSet :

  1. Convert LinkedHashSet to TreeSet for natural-ordering
  2. Convert LinkedHashSet to TreeSet for reverse-ordering by providing Comparator based customized-sorting logic
  3. Using ArrayList and Collections class’ utility method sort();

Let us move forward to discuss different ways to sort LinkedHashSet contents

1. Convert LinkedHashSet to TreeSet for natural-ordering

  • Create new LinkedHashSet object
  • Store/add contents to LinkedHashSet
  • Create new TreeSet object,
  • Pass LinkedHashSet contents as argument to TreeSet’ inter-conversion constructor

Syntax:

// convert LinkedHashSet to TreeSet
		TreeSet<String> tSet = new TreeSet<String>(lhsCompanies); 

SortingLinkedHashSetUsingTreeSet.java

package in.bench.resources.collection;

import java.util.LinkedHashSet;
import java.util.TreeSet;

public class SortingLinkedHashSetUsingTreeSet {

	public static void main(String[] args) {

		// creating LinkedHashSet object of type String
		LinkedHashSet<String> lhsCompanies =
				new LinkedHashSet<String>();


		// adding elements to LinkedHashSet object
		lhsCompanies.add("LinkedIn");
		lhsCompanies.add("Amazon");
		lhsCompanies.add("Google");
		lhsCompanies.add("Apple");
		lhsCompanies.add("Facebook");
		lhsCompanies.add("Oracle");
		lhsCompanies.add("Microsoft");


		// Iterating using enhanced for-loop
		System.out.println("Insertion Order:"
				+ " Iterating LinkedHashSet\n");
		for(String company : lhsCompanies) {
			System.out.println(company);
		}


		// convert LinkedHashSet to TreeSet
		TreeSet<String> tSet = new TreeSet<String>(lhsCompanies); 


		// Iterating using enhanced for-loop
		System.out.println("\n\nAscending sorting-order:"
				+ " Iterating TreeSet\n");
		for(String company : tSet) {
			System.out.println(company);
		}
	}
}

Output:

Insertion Order: Iterating LinkedHashSet

LinkedIn
Amazon
Google
Apple
Facebook
Oracle
Microsoft


Ascending sorting-order: Iterating TreeSet

Amazon
Apple
Facebook
Google
LinkedIn
Microsoft
Oracle

2. Convert LinkedHashSet to TreeSet for reverse-ordering

  • Create new LinkedHashSet object
  • Store/add contents to LinkedHashSet
  • Create new TreeSet object and provide reverse-order sorting-logic using Comparator
  • Add LinkedHashSet contents  to TreeSet using addAll(); method

Syntax:

// create new TreeSet object and provide reverse-sorting logic
		TreeSet<String> tSet = new TreeSet<String>(
				new Comparator<String>() {

					@Override
					public int compare(String str1, String str2) {
						return str2.compareTo(str1);
					}
				}); 

ReverseSortingLinkedHashSetUsingTreeSet.java

package in.bench.resources.collection;

import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.TreeSet;

public class ReverseSortingLinkedHashSetUsingTreeSet {

	public static void main(String[] args) {

		// creating LinkedHashSet object of type String
		LinkedHashSet<String> lhsCompanies =
				new LinkedHashSet<String>();


		// adding elements to LinkedHashSet object
		lhsCompanies.add("LinkedIn");
		lhsCompanies.add("Amazon");
		lhsCompanies.add("Google");
		lhsCompanies.add("Apple");
		lhsCompanies.add("Facebook");
		lhsCompanies.add("Oracle");
		lhsCompanies.add("Microsoft");


		// Iterating using enhanced for-loop
		System.out.println("Insertion Order:"
				+ " Iterating LinkedHashSet\n");
		for(String company : lhsCompanies) {
			System.out.println(company);
		}


		// create new TreeSet object and provide reverse-sorting logic
		TreeSet<String> tSet = new TreeSet<String>(
				new Comparator<String>() {

					@Override
					public int compare(String str1, String str2) {
						return str2.compareTo(str1);
					}
				}); 


		// add LinkedHashSet to TreeSet for reverse-sorting elements
		tSet.addAll(lhsCompanies);


		// Iterating using enhanced for-loop
		System.out.println("\n\nDescending sorting-order:"
				+ " Iterating TreeSet\n");
		for(String company : tSet) {
			System.out.println(company);
		}
	}
}

Output:

Insertion Order: Iterating LinkedHashSet

LinkedIn
Amazon
Google
Apple
Facebook
Oracle
Microsoft


Descending sorting-order: Iterating TreeSet

Oracle
Microsoft
LinkedIn
Google
Facebook
Apple
Amazon

3. Using ArrayList and Collections’ class utility method sort()

  • Create new LinkedHashSet object
  • Store/add contents to LinkedHashSet
  • Convert LinkedHashSet to ArrayList using inter-conversion constructor
  • Invoke sort(); method on ArrayList contents using Collections‘ class
  • Invoke clear(); method on original LinkedHashSet, if we want to convert back ArrayList to LinkedHashSet
  • Finally, convert ArrayList to LinkedHashSet by adding ArrayList contents to LinkedHashSet, as LinkedHashSet maintains insertion-order, if we want to retain original LinkedHashSet
  • Note : we can sort in reverse-order using Comparator and providing reverse-order sorting logic

Syntax:

// convert LinkedHashSet to ArrayList
		List<String> lst = new ArrayList<String>(lhsCompanies);


		// sort using Collections class
		Collections.sort(lst);


		// clear or empty LinkedHashSet
		lhsCompanies.clear();


		// convert sorted ArrayList to LinkedHashSet, afetr clear();
		lhsCompanies.addAll(lst);

SortingLinkedHashSetUsingArrayList.java

package in.bench.resources.collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

public class SortingLinkedHashSetUsingArrayList {

	public static void main(String[] args) {

		// creating LinkedHashSet object of type String
		LinkedHashSet<String> lhsCompanies =
				new LinkedHashSet<String>();


		// adding elements to LinkedHashSet object
		lhsCompanies.add("LinkedIn");
		lhsCompanies.add("Amazon");
		lhsCompanies.add("Google");
		lhsCompanies.add("Apple");
		lhsCompanies.add("Facebook");
		lhsCompanies.add("Oracle");
		lhsCompanies.add("Microsoft");


		// Iterating using enhanced for-loop
		System.out.println("Insertion Order:"
				+ " Iterating LinkedHashSet\n");
		for(String company : lhsCompanies) {
			System.out.println(company);
		}


		// convert LinkedHashSet to ArrayList
		List<String> lst = new ArrayList<String>(lhsCompanies);


		// sort using Collections class
		Collections.sort(lst);


		// clear or empty LinkedHashSet
		lhsCompanies.clear();


		// convert sorted ArrayList to LinkedHashSet, afetr clear();
		lhsCompanies.addAll(lst);


		// Iterating using enhanced for-loop
		System.out.println("\n\nAscending sorting-order:"
				+ " Iterating LinkedHashSet\n");
		for(String company : lhsCompanies) {
			System.out.println(company);
		}
	}
}

Output:

Insertion Order: Iterating LinkedHashSet

LinkedIn
Amazon
Google
Apple
Facebook
Oracle
Microsoft


Ascending sorting-order: Iterating LinkedHashSet

Amazon
Apple
Facebook
Google
LinkedIn
Microsoft
Oracle

References:

Happy Coding !!
Happy Learning !!

Java - How to sort LinkedHashMap by its Keys ?
Java - 5 ways to iterate through TreeMap