Java – TreeSet class with example

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, 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
  • 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:

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){


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:


Happy Coding !!
Happy Learning !!

Java - List v/s Set
Java 6 - NavigableSet interface with example