Convert HashSet to TreeSet in Java

03 Jan, 2019
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.First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.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]

First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.Finally, create an object for the tree set and send the hash set object to it.
First, we have to create an object for the hash set.
Then we have to add all the elements to the hash set.
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);    }}
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.First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.Now create an object for the treeset .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]

First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.Now create an object for the treeset .Using addAll method add all elements of hash set to it.
First, we have to create an object for the hash set.
Then we have to add all the elements to the hash set.
Now create an object for the treeset .
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);    }}
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.)First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.Now create an object for the treeset .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]

First, we have to create an object for the hash set.Then we have to add all the elements to the hash set.Now create an object for the treeset .Finally, by using for each loop adding all elements of hash set to the tree set.
First, we have to create an object for the hash set.
Then we have to add all the elements to the hash set.
Now create an object for the treeset .
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);    }}
HashSet: [Geeks, For, Welcome, To]
TreeSet: [For, Geeks, To, Welcome]

Java - util package
Java-Collections
java-hashset
Java-Set-Programs
java-treeset
Picked
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java
For-each loop in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java