This example shows how to add elements to TreeSet in Java (including custom class objects). This example also shows how to add elements using the add method and addAll method.
How to add elements to TreeSet in Java using the add method?
The add
method of the TreeSet class adds the specified element to the TreeSet object.
1 |
public boolean add(E e) |
The add
method adds the specified element to the TreeSet object if it was not already present in the set and returns true. If the element was already present in the set, the set remains unchanged and it returns false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.TreeSet; public class TreeSetAddElementsExample { public static void main(String[] args) { TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); /* * Use the TreeSet add method to add elements * to TreeSet object */ //this will add element "1" to the TreeSet and return true System.out.println( tSetOddNumbers.add(1) ); //this will return false as element "1" already exists System.out.println( tSetOddNumbers.add(1) ); System.out.println("TreeSet contains: " + tSetOddNumbers); } } |
Output
1 2 3 |
true false TreeSet contains: [1] |
How to add custom class objects to the TreeSet?
Let’s create a Book custom class and try to add its objects to a TreeSet using the add
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.TreeSet; class Book{ private int bookId; public Book(int bookId){ this.bookId = bookId; } public int getBookId(){ return this.bookId; } public String toString(){ return "Book -> " + getBookId(); } } public class TreeSetAddElementsExample { public static void main(String[] args) { TreeSet<Book> tSetBooks = new TreeSet<Book>(); tSetBooks.add( new Book(1) ); } } |
Output
1 2 3 4 |
Exception in thread "main" java.lang.ClassCastException: com.javacodeexamples.collections.treeset.Book cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.TreeSet.add(Unknown Source) |
The code throws “java.lang.ClassCastException: com.javacodeexamples.collections.treeset.Book cannot be cast to java.lang.Comparable” when we run it.
The TreeSet class sorts the elements according to the natural ordering of the elements or by the specified Comparator object. In other words, all the elements of the TreeSet either must implement the Comparable interface or a custom comparator must be provided in the TreeSet constructor.
Let’s implement the Comparable interface in the Book class and try again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.TreeSet; /* * Implement the Comparable interface */ class Book implements Comparable<Book>{ private int bookId; public Book(int bookId){ this.bookId = bookId; } public int getBookId(){ return this.bookId; } public String toString(){ return "Book -> " + getBookId(); } /* * define the compareTo method */ public int compareTo(Book otherBook) { return getBookId() - otherBook.getBookId(); } } public class TreeSetAddElementsExample { public static void main(String[] args) { TreeSet<Book> tSetBooks = new TreeSet<Book>(); //this will add Book with id 1 and return true System.out.println( tSetBooks.add( new Book(1) ) ); //this will return false as book with id 1 already exists System.out.println( tSetBooks.add( new Book(1) ) ); System.out.println("TreeSet contains: " + tSetBooks); } } |
Output
1 2 3 |
true false TreeSet contains: [Book -> 1] |
How to add all elements of other collections to the TreeSet using the addAll method?
The addAll
method of the TreeSet class adds all the elements of the specified collection to this set.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method returns true if the set is changed as a result of this method call. The addAll
method can be used to convert a collection like HashSet to TreeSet as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
HashSet<Integer> hashSetNumbers = new HashSet<Integer>(); hashSetNumbers.add(3); hashSetNumbers.add(2); hashSetNumbers.add(1); /* * Use addAll method to convert HashSet to TreeSet */ //create new TreeSet TreeSet<Integer> tSetNumbers = new TreeSet<Integer>(); tSetNumbers.addAll(hashSetNumbers); System.out.println("TreeSet contains: " + tSetNumbers); |
Output
1 |
TreeSet contains: [1, 2, 3] |
This example is a part of the Java TreeSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet