Convert HashSet to TreeSet in Java

Hashset: Hashset in Java is generally used for operations like search, insert and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table.

TreeSet: TreeSet in Java takes O(log n) for search, insert and delete which is higher than HashSet. But TreeSet keeps sorted data. Also, it supports operations like higher() (Returns least higher element), floor(), ceiling(), etc. These operations are also O(log n) in TreeSet and not supported in HashSet. TreeSet is implemented using a Self Balancing Binary Search Tree (Red-Black Tree). TreeSet is backed by TreeMap in Java.

In general, if you want a sorted set then it is better to add elements to HashSet and then convert it into TreeSet rather than creating a TreeSet and adding elements to it.

Given a HashSet the task is to convert it into TreeSet in Java.

Examples:

HashSet: [Geeks, For, Welcome, To]
TreeSet: [For, Geeks, To, Welcome]

HashSet: [1, 2, 3, 4, 5]
TreeSet: [1, 2, 3, 4, 5]

We can convert HashSet to TreeSet in following ways:

  • By invoking the parameterized constructor and sending object of Hash set as a parameter to it.
    1. First, we have to create an object for the hash set.
    2. Then we have to add all the elements to the hash set.
    3. Finally, create an object for the tree set and send the hash set object to it.

    Below is the implementation of the above approach:

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.HashSet;
    import java.util.Set;
    import java.util.TreeSet;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Get the HashSet
            Set<String> setobj = new HashSet<>();
            setobj.add("Welcome");
            setobj.add("To");
            setobj.add("Geeks");
            setobj.add("For");
            setobj.add("Geeks");
      
            System.out.println("HashSet: "
                               + setobj);
      
            // Convert the HashSet to TreeSet
            Set<String> hashSetToTreeSet
                = new TreeSet<>(setobj);
      
            // Print the TreeSet
            System.out.println("TreeSet: "
                               + hashSetToTreeSet);
        }
    }

    chevron_right

    
    

    Output:

    HashSet: [Geeks, For, Welcome, To]
    TreeSet: [For, Geeks, To, Welcome]
    
  • By constructing a tree set containing the same elements present in the hash set by using addAll method.
    1. First, we have to create an object for the hash set.
    2. Then we have to add all the elements to the hash set.
    3. Now create an object for the treeset .
    4. Using addAll method add all elements of hash set to it.

    Below is the implementation of the above approach:

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.HashSet;
    import java.util.Set;
    import java.util.TreeSet;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Get the HashSet
            Set<String> setobj = new HashSet<>();
            setobj.add("Welcome");
            setobj.add("To");
            setobj.add("Geeks");
            setobj.add("For");
            setobj.add("Geeks");
      
            System.out.println("HashSet: "
                               + setobj);
      
            // Convert the HashSet to TreeSet
            Set<String> hashSetToTreeSet
                = new TreeSet<>();
            hashSetToTreeSet.addAll(setobj);
      
            // Print the TreeSet
            System.out.println("TreeSet: "
                               + hashSetToTreeSet);
        }
    }

    chevron_right

    
    

    Output:

    HashSet: [Geeks, For, Welcome, To]
    TreeSet: [For, Geeks, To, Welcome]
    
  • By using a for each loop.( This method is commonly used for conversion between two incompatible types.)
    1. First, we have to create an object for the hash set.
    2. Then we have to add all the elements to the hash set.
    3. Now create an object for the treeset .
    4. Finally, by using for each loop adding all elements of hash set to the tree set.

    Below is the implementation of the above approach:

    Program:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.HashSet;
    import java.util.Set;
    import java.util.TreeSet;
      
    public class GFG {
      
        public static void main(String[] args)
        {
      
            // Get the HashSet
            Set<String> setobj = new HashSet<>();
            setobj.add("Welcome");
            setobj.add("To");
            setobj.add("Geeks");
            setobj.add("For");
            setobj.add("Geeks");
      
            System.out.println("HashSet: "
                               + setobj);
      
            // Convert the HashSet to TreeSet
            Set<String> hashSetToTreeSet
                = new TreeSet<>();
            for (String i : setobj)
                hashSetToTreeSet
                    .add(i);
      
            // Print the TreeSet
            System.out.println("TreeSet: "
                               + hashSetToTreeSet);
        }
    }

    chevron_right

    
    

    Output:

    HashSet: [Geeks, For, Welcome, To]
    TreeSet: [For, Geeks, To, Welcome]
    


  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.