This example shows how to compare two TreeSet objects in Java. This example also shows how to compare two TreeSet objects using the TreeSet equals method.
How to compare two TreeSet objects in Java using the equals method?
We can check if two TreeSet objects are equal or not using the equals
method. The TreeSet class inherits the equals
method from the parent class AbstractSet.
1 |
public boolean equals(Object o) |
The equals
method of the TreeSet compares the specified object with this TreeSet object for equality. It returns true if the specified object is a set, both have the same number of elements (having the same size), and every element of the specified set object is also contained in this TreeSet object (using the containsAll
method).
In other words, the equals
method of the TreeSet class compares two TreeSet objects by elements. If both have the same elements, it returns true, false otherwise.
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 |
import java.util.TreeSet; public class CompareTreeSetExample { public static void main(String[] args) { //first TreeSet TreeSet<String> tSetCharacters1 = new TreeSet<String>(); tSetCharacters1.add("c"); tSetCharacters1.add("d"); tSetCharacters1.add("b"); tSetCharacters1.add("a"); //second TreeSet TreeSet<String> tSetCharacters2 = new TreeSet<String>(); tSetCharacters2.add("c"); tSetCharacters2.add("d"); tSetCharacters2.add("b"); tSetCharacters2.add("a"); /* * To compare two TreeSet objects, use * the equals method */ //this will return true as both are equal System.out.println(tSetCharacters1.equals(tSetCharacters2)); //remove an element from second TreeSet tSetCharacters2.remove("d"); //this will return false as both are not equal now System.out.println(tSetCharacters1.equals(tSetCharacters2)); } } |
Output
1 2 |
true false |
How to compare TreeSet objects having elements of a custom class?
Let’s first see an example of that.
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 |
import java.util.TreeSet; class Document{ private int documentId; public Document(int documentId){ this.documentId = documentId; } public int getDocumentId(){ return this.documentId; } public String toString(){ return "Document - " + getDocumentId(); } } public class CompareTreeSetExample { public static void main(String[] args) { TreeSet<Document> tSetDocuments1 = new TreeSet<Document>(); tSetDocuments1.add(new Document(1)); tSetDocuments1.add(new Document(2)); TreeSet<Document> tSetDocuments2 = new TreeSet<Document>(); tSetDocuments2.add(new Document(1)); tSetDocuments2.add(new Document(2)); System.out.println( tSetDocuments1.equals(tSetDocuments2) ); } } |
Output
1 2 3 4 |
Exception in thread "main" java.lang.ClassCastException: com.javacodeexamples.collections.treeset.Document 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) |
As we can see from the output, the code threw java.lang.ClassCastException exception. The reason is, all elements of the TreeSet either must implement the Comparable interface and define the compareTo
method or a custom Comparator must be provided in the TreeSet constructor.
Let’s implement the Comparable interface in the Document 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 |
import java.util.TreeSet; //implement the Comparable interface class Document implements Comparable<Document>{ private int documentId; public Document(int documentId){ this.documentId = documentId; } public int getDocumentId(){ return this.documentId; } public String toString(){ return "Document - " + getDocumentId(); } //override the compareTo method public int compareTo(Document otherDocument) { return this.getDocumentId() - otherDocument.getDocumentId(); } } public class CompareTreeSetExample { public static void main(String[] args) { TreeSet<Document> tSetDocuments1 = new TreeSet<Document>(); tSetDocuments1.add(new Document(1)); tSetDocuments1.add(new Document(2)); TreeSet<Document> tSetDocuments2 = new TreeSet<Document>(); tSetDocuments2.add(new Document(1)); tSetDocuments2.add(new Document(2)); System.out.println( tSetDocuments1.equals(tSetDocuments2) ); } } |
Output
1 |
true |
It worked as expected this time.
Tip: Always override the toString
, equals
, hashCode
methods in your custom class.
This example is a part of the TreeSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeSet