A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/how-to-sort-hashset-in-java/ below:

How to sort HashSet in Java

How to sort HashSet in Java

Last Updated : 11 Jul, 2025

Given a HashSet in Java, the task is to sort this HashSet.

Examples:
Input: HashSet: [Geeks, For, ForGeeks, GeeksforGeeks]
Output: [For, ForGeeks, Geeks, GeeksforGeeks]

Input: HashSet: [2, 5, 3, 1, 4]
Output: [1, 2, 3, 4, 5] 

The

HashSet class

implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time.

It means that HashSet does not maintains the order of its elements. Hence sorting of HashSet is not possible.

However, the elements of the HashSet can be sorted indirectly by converting into List or TreeSet, but this will keep the elements in the target type instead of HashSet type. Below is the implementation of the above approach:

Program 1:

By Converting HashSet to List.

Java
// Java program to sort a HashSet

import java.util.*;

public class GFG {
    public static void main(String args[])
    {
        // Creating a HashSet
        HashSet<String> set = new HashSet<String>();

        // Adding elements into HashSet using add()
        set.add("geeks");
        set.add("practice");
        set.add("contribute");
        set.add("ide");

        System.out.println("Original HashSet: "
                           + set);

        // Sorting HashSet using List
        List<String> list = new ArrayList<String>(set);
        Collections.sort(list);

        // Print the sorted elements of the HashSet
        System.out.println("HashSet elements "
                           + "in sorted order "
                           + "using List: "
                           + list);
    }
}
Output:
Original HashSet: [practice, geeks, contribute, ide]
HashSet elements in sorted order using List: [contribute, geeks, ide, practice]
Program 2:

By Converting HashSet to TreeSet.

Java
// Java program to sort a HashSet

import java.util.*;

public class GFG {
    public static void main(String args[])
    {

        // Creating a HashSet
        HashSet<String> set = new HashSet<String>();

        // Adding elements into HashSet using add()
        set.add("geeks");
        set.add("practice");
        set.add("contribute");
        set.add("ide");

        System.out.println("Original HashSet: "
                           + set);

        // Sorting HashSet using TreeSet
        TreeSet<String> treeSet = new TreeSet<String>(set);

        // Print the sorted elements of the HashSet
        System.out.println("HashSet elements "
                           + "in sorted order "
                           + "using TreeSet: "
                           + treeSet);
    }
}
Output:
Original HashSet: [practice, geeks, contribute, ide]
HashSet elements in sorted order using TreeSet: [contribute, geeks, ide, practice]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4