Open In App

Convert HashSet to TreeSet in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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:




    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);
        }
    }

    
    

    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:




    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);
        }
    }

    
    

    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:




    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);
        }
    }

    
    

    Output:

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


  • Last Updated : 03 Jan, 2019
    Like Article
    Save Article
    Previous
    Next
    Share your thoughts in the comments
    Similar Reads