How to Add Custom Class Objects to the TreeSet in Java? Last Updated : 24 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report TreeSet is an implementation of the SortedSet interface in java that uses a red-black tree for storage. By default, It maintains an ascending order. It contains unique elements only. It doesn't allow null elements. Access and retrieval times are quite fast. To add the user-defined object into TreeSet, we need to implement a Comparable interface. An element that we want to add in TreeSet must be a comparable type. If we don't implement the Comparable interface then it will throw ClassCastException. Example 1 Java // Importing util package import java.util.*; // Custom class Car implements Comparable interface class Car implements Comparable<Car> { // attributes int Modelno; String name, city; int stock; // Car constructor public Car(int Modelno, String name, String city, int stock) { this.Modelno = Modelno; this.name = name; this.city = city; this.stock = stock; } // Override the compareTo() method public int compareTo(Car c) { if (stock > c.stock) { return 1; } else if (stock < c.stock) { return -1; } else { return 0; } } } public class GFG { // Main driver method public static void main(String[] args) { // Define an objects of TreeSet class TreeSet<Car> set = new TreeSet<Car>(); // Creating Car objects Car c1 = new Car(132, "BMW", "Rajkot", 35); Car c2 = new Car(269, "Audi", "Surat", 20); Car c3 = new Car(560, "Kia", "Vadodara", 15); Car c4 = new Car(109, "Creta", "Ahmedabad", 26); // Adding Car objects to TreeSet set.add(c1); set.add(c2); set.add(c3); set.add(c4); // Traversing TreeSet for (Car c : set) { System.out.println(c.stock + " " + c.name + " " + c.city + " " + c.Modelno); } } } Output15 Kia Vadodara 560 20 Audi Surat 269 26 Creta Ahmedabad 109 35 BMW Rajkot 132 We need to override the compareTo() method so that it will sort our set in a particular order. Example 2: Let's take the same example but now we override the compareTo() method with respect to the name of the car. Java // Importing util package import java.util.*; // Custom class Car implements Comparable interface class Car implements Comparable<Car> { // attributes int Modelno; String name, city; int stock; // Car constructor public Car(int Modelno, String name, String city, int stock) { this.Modelno = Modelno; this.name = name; this.city = city; this.stock = stock; } // Override the compareTo() method public int compareTo(Car c) { return name.compareTo(c.name); } } public class GFG { // Main driver method public static void main(String[] args) { // Define an objects of TreeSet class TreeSet<Car> set = new TreeSet<Car>(); // Creating Car objects Car c1 = new Car(132, "BMW", "Rajkot", 35); Car c2 = new Car(269, "Audi", "Surat", 20); Car c3 = new Car(560, "Kia", "Vadodara", 15); Car c4 = new Car(109, "Creta", "Ahmedabad", 26); // Adding Car objects to TreeSet set.add(c1); set.add(c2); set.add(c3); set.add(c4); // Traversing TreeSet for (Car c : set) { System.out.println(c.name + " " + c.stock + " " + c.city + " " + c.Modelno); } } } OutputAudi 20 Surat 269 BMW 35 Rajkot 132 Creta 26 Ahmedabad 109 Kia 15 Vadodara 560 Comment More infoAdvertise with us Next Article How to Add or Append Elements to End of a TreeSet in Java? M meetsuvariya Follow Improve Article Tags : Java Technical Scripter Java Programs java-treeset Practice Tags : Java Similar Reads How to Create a TreeSet with a List in Java? TreeSet is an implementation of the SortedSet interface in Java that uses a Tree for storage. TreeSet can be created from List by passing the List to the TreeSet constructor in Java or we can traverse complete List and adding each element of the List to the TreeSet. Example: Input : List = [a, b, c] 3 min read How to Sort a TreeSet with User Defined Objects in Java? Comparator interface sorts the objects of user-defined classes. An object of the Comparator class is capable of comparing two objects of two different classes. Following function compare obj1 with obj2 TreeSet implements the SortedSet interface. So, duplicate values are not allowed.Objects in a Tree 3 min read How to Avoid Duplicate User Defined Objects in TreeSet in Java? TreeSet class in Java is part of Javaâs collections framework which implements the NavigableSet interface, which provides functionalities to navigate through the SortedSet. The NavigableSet further extends the SortedSet interface, which provides functionalities to keep the elements sorted. As the Tr 5 min read How to Change the Comparator to Return a Descending Order in Java TreeSet? Comparator interface is used to order the objects of user-defined classes. A comparator object is capable of comparing two objects of two different classes. TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage. The ordering of the eleme 3 min read How to Add or Append Elements to End of a TreeSet in Java? A TreeSet is a sorted set implementation of the Set interface based on a TreeMap. It uses a Red-Black tree to store elements. Elements are sorted according to their natural ordering, or a custom Comparator provided at the time of TreeSet creation. It does not allow duplicate elements or null values. 2 min read How to sort TreeSet in descending order in Java? Given a TreeSet in Java, task is to sort elements of TreeSet in Descending Order (descreasing order).Examples: Input : Set: [2, 3, 5, 7, 10, 20] Output : Set: [20, 10, 7, 5, 3, 2] Input : Set: [computer, for, geeks, hello] Output : Set: [hello, geeks, for, computer] Approach: To make a TreeSet Eleme 1 min read Like