In this article, we will discuss TreeSet class – the implementation class for NavigableSet interface in detail
1. Key points about TreeSet:
- TreeSet doesn’t allow duplicate items
- maintains ascending sorting-order, by default
2. TreeSet:
- TreeSet is the implementation class of NavigableSet interface (i.e.; TreeSet implements NavigableSet)
- TreeSet uses balanced-tree to store element/objects
- Duplicate element/objects are NOT allowed
- If duplicate value is added again, then there won’t be any compile-time or runtime errors
- Simply add(object) method returns false for already containing object inside TreeSet
- At any time, TreeSet contains only unique element/objects
- Insertion-order is NOT maintained
- But TreeSet stores element/objects in sorting-order; it could be either default natural sorting order or programmer defined customized sorting-order
- Allows NULL insertion but maximum of only one NULL value (till Java 1.6 version)
- From Java 1.7 version, NULL insertion is not possible
- TreeSet is non-synchronized
- Present in java.util package and extends java.util.AbstractSet implements java.util.NavigableSet interface
- Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to TreeSet (provided by JVM at run time) like,
- java.lang.Cloneable: to create a duplicate object or to clone an object
- java.io.Serializable: to transfer objects across network
Source: Team BenchResources.Net
3. TreeSet constructors:
3.1 TreeSet ts = new TreeSet();
- creates an empty TreeSet object
- where elements will be inserted according to default natural sorting order
3.2 TreeSet ts = new TreeSet(Comparator c);
- creates an empty TreeSet object
- where elements will be inserted according to specified comparator object (i.e.; customized sorting order)
3.3 TreeSet ts = new TreeSet(Collection c);
- creates an equivalent TreeSet object for the specified collection
- it is basically used for inter-conversion between collection objects
3.4 TreeSet ts = new TreeSet(SortedSet s);
- create an equivalent TreeSet object for the specified SortedSet
- follows same ordering as per specified SortedSet
4. TreeSet examples:
TreeSetStringExample.java
package in.bench.resources.collection;
import java.util.TreeSet;
public class TreeSetStringExample {
public static void main(String[] args) {
// creating TreeSet object of type String
TreeSet<String> ts = new TreeSet<String>();
// adding elements to TreeSet object
ts.add("Sundar Pichai");
ts.add("Satya Nadella");
ts.add("Shiv Nadar");
ts.add("Shantanu Narayen");
ts.add("Sundar Pichai"); // adding duplicate element
ts.add("Francisco D’Souza");
ts.add("Vishal Sikka");
ts.add("Chanda Kochhar");
// according to natural ordering
System.out.println("Sorted according"
+ " to Natural-ordering:\n");
for(String CEO : ts){
System.out.println(CEO);
}
}
}
Output:
Sorted according to Natural-ordering:
Chanda Kochhar
Francisco D’Souza
Satya Nadella
Shantanu Narayen
Shiv Nadar
Sundar Pichai
Vishal Sikka
Note: All methods of TreeSet is non-synchronized
Q) How to make TreeSet synchronized ?
- TreeSet can be easily converted into synchronized TreeSet
- Using utility method synchronizedSet(ts); of java.util.Collections class
Set set = Collections.synchronizedSet(ts);
Related Articles:
- Set interface
- HashSet class
- LinkedHashSet class
- SortedSet interface
- NavigableSet interface
- TreeSet class
- HashSet v/s LinkedHashSet
- HashSet v/s LinkedHashSet v/s TreeSet
- List v/s Set
- HashMap v/s HashSet
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html
- https://docs.oracle.com/javase/7/docs/api/java/util/SortedSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/NavigableSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/class-use/NavigableSet.html
- https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html
- https://docs.oracle.com/cd/E21764_01/apirefs.1111/e17503/oracle/adfnmc/java/util/TreeSet.html
- https://docs.oracle.com/cd/E21764_01/apirefs.1111/e17503/index.html?oracle/adfnmc/java/util/TreeSet.html
Happy Coding !!
Happy Learning !!