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]