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:
- 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]
- 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]
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]
Recommended Posts:
- HashSet vs TreeSet in Java
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- HashSet in Java
- Traverse through a HashSet in Java
- HashSet add() Method in Java
- How to sort HashSet in Java
- Initializing HashSet in Java
- HashSet contains() Method in Java
- Internal working of Set/HashSet in Java
- HashSet equals() method in Java with Example
- HashSet spliterator() method in Java
- HashSet clear() Method in Java
- HashSet toArray(T[]) method in Java with Example
- HashSet removeAll() method in Java with Example
- HashSet toArray() method in Java with Example
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.


