TreeSet with user defined objects in Java

TreeSet with user defined objects in Java

TreeSet is an implementation of the Set interface that uses a TreeMap for storing elements. TreeSet implements SortedSet interface thus the elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on the constructor used for creating Set. TreeSet does not allow duplicate elements (as is the case with any Set implementation). In this post we will see example usage of TreeSet with user defined objects in Java.

Hierarchy of TreeSet class

Below is the hierarchy of TreeSet class in Java.

TreeSet in Java

TreeSet with custom objects

In order to add custom or user defined objects to TreeSet data structure, the custom class should implement the Comparable interface or a Comparator should be passed to TreeSet constructor during creation. This is required because TreeSet does not allow duplicate elements and to ensure this with custom objects, TreeSet requires a comparator function to perform the comparison of custom objects. If a comparator is not specified then when you try to add a custom object to TreeSet, it will throw java.lang.ClassCastException. This is the most important point to be noted regarding the usage of TreeSet with custom objects in Java.

Let us see an example program to add custom objects to TreeSet data structure.

Below is the output of running the above program.

If you check the output, you can notice that there is only one entry for the employee “Arun”. However we have added the employee “Arun” two times to the TreeSet. Thus we have ensured that duplicates are not allowed in TreeSet . This is done by implementing Comparable interface in the Employee class. The compareTo() method in the Employee class defines how two employee objects should be compared. We have defined two employee objects to be equal if their empId is equal.

The compareTo() method is defined such that the employee objects are sorted in ascending order of empId (which is evident in the output).

User defined objects can also be added to TreeSet by passing a Comparator to the constructor function during TreeSet creation. In such case the custom class need not implement Comparable interface.

Here is a Java program that shows how to add custom objects to TreeSet with a comparator defined.

Below is the output of running the above program.

Try to run the above program by removing the Comparator in the TreeSet constructor. You will end up with an exception as below,

          Exception in thread “main” java.lang.ClassCastException: Employee cannot be cast to
          at java.util.TreeMap.put(
          at java.util.TreeSet.add(
          at posts.TreeSetCustomObject.main(

Hope you have learned how to use TreeSet with user defined objects in Java.

If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.