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:

  • 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, java.io.Serializable 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

20-TreeSet-interace-in-java

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:

 

TreeSetStringExample.java

package in.bench.resources.java.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");
		System.out.println(ts);
	}
}

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

Set set = Collections.synchronizedSet(ts);

 

 

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
http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.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 !!