TreeSet class

In this article, we will discuss TreeSet class – the implementation class for NavigableSet interface in detail

TreeSet doesn’t allow duplicate items and maintains ascending sorting order, by default



  • 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 and again, 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 up-to 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)

Cloneable: to create a duplicate object or to clone an object

Serializable: to transfer objects across network


Source: Team BenchResources.Net


TreeSet constructors:

TreeSet ts = new TreeSet();

  • creates an empty TreeSet object, where elements will be inserted according to default natural sorting order


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)


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


TreeSet ts = new TreeSet(SortedSet s);

  • create an equivalent TreeSet object for the specified SortedSet
  • follows same ordering as per specified SortedSet


TreeSet examples:


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");


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 but it can be easily converted into synchronized TreeSet using utility method of java.util.Collections class

Set set = Collections.synchronizedSet(ts);





Happy Coding !!
Happy Learning !!

List v/s Set
NavigableSet interface