Convert HashSet to TreeSet in Java
Last Updated :
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:
Java
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.
- 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:
Java
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.)
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:
Java
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]
Similar Reads
Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat
14 min read
AbstractSet Class in Java In Java, the AbstractSet class is part of the Java Collections Framework. It provides a Skeleton implementation of the set interface, which is a collection that does not allow duplicate elements. This class is abstract, meaning it cannot be instantiated directly, but it can be extended to create a c
8 min read
EnumSet in Java In Java, the EnumSet is a specialized set implementation for use with enum types. It is a part of java.util package and provides a highly optimized set for storing enum constants. The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type.It extends
9 min read
Java HashSet HashSet in Java implements the Set interface of Collections Framework. It is used to store the unique elements and it doesn't maintain any specific order of elements. Can store the Null values.Uses HashMap (implementation of hash table data structure) internally.Also implements Serializable and Clon
12 min read
TreeSet in Java TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree(red - black tree) for storage. The ordering of the elements is maintained by a set using their natural ordering whether or not an explicit comparator is provided. This must be consistent with equ
13 min read
ConcurrentSkipListSet in Java In Java, the ConcurrentSkipListSet is the part of the java.util.concurrent package and provides a scalable, thread-safe alternative to TreeSet. It is a sorted set that lets multiple threads safely access and modify the set at the same time without causing issues.It is thread-safe.Elements are in sor
7 min read
CopyOnWriteArraySet in Java In Java, the CopyOnWriteArraySet is the part of the java.util.concurrent package and is used to handle thread-safe operations in multi-threaded environments. It is ideal when the set is frequently read but infrequently modified. The set ensures safe access for multiple threads, as it creates a new c
6 min read
Java LinkedHashSet LinkedHashSet in Java implements the Set interface of the Collection Framework. It combines the functionality of a HashSet with a LinkedList to maintain the insertion order of elements. Stores unique elements only.Maintains insertion order.Provides faster iteration compared to HashSet.Allows null el
8 min read
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
3 min read
Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java In this article, we will learn, the difference between HashSet vs LinkedHashSet and TreeSet And similarities between LinkedHashSet and TreeSet. HashSet, LinkedHashSet, and TreeSet all implement the Set interface. So we have tried to list out the differences and similarities between HashSet, LinkedHa
6 min read