The equals() method of java.util.TreeSet class is used to compare the specified object with this set for equality. Returns true if and only if the specified object is also a set, both sets have the same size, and all corresponding pairs of elements in the two sets are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two sets are defined to be equal if they contain the same elements in the same order.
Syntax:
public boolean equals(Object o)
Parameters: This method takes the object o as a parameter to be compared for equality with this set.
Returns Value: This method returns true if the specified object is equal to this set.
Below are the examples to illustrate the equals() method.
Example 1:
// Java program to demonstrate equals() // method of TreeSet import java.util.*; public class GFG { public static void main(String[] argv) { // Creating object of TreeSet<String> TreeSet<String> set1 = new TreeSet<String>(); // Populating set1 set1.add("A"); set1.add("B"); set1.add("C"); set1.add("D"); set1.add("E"); // print set1 System.out.println("First TreeSet: " + set1); // Creating another object of TreeSet<String> TreeSet<String> set2 = new TreeSet<String>(); // Populating set2 set2.add("A"); set2.add("B"); set2.add("C"); set2.add("D"); set2.add("E"); // print set2 System.out.println("Second TreeSet: " + set2); // comparing first TreeSet to another // using equals() method boolean value = set1.equals(set2); // print the value System.out.println("Are both set equal: " + value); } } |
First TreeSet: [A, B, C, D, E] Second TreeSet: [A, B, C, D, E] Are both set equal: true
Example 2:
// Java program to demonstrate equals() // method of TreeSet import java.util.*; public class GFG1 { public static void main(String[] argv) { // Creating object of TreeSet TreeSet<Integer> set1 = new TreeSet<Integer>(); // Populating set1 set1.add(10); set1.add(20); set1.add(30); set1.add(40); set1.add(50); // print set1 System.out.println("First TreeSet: " + set1); // Creating another object of TreeSet TreeSet<Integer> set2 = new TreeSet<Integer>(); // Populating set2 set2.add(10); set2.add(20); set2.add(30); // print set2 System.out.println("Second TreeSet: " + set2); // comparing first TreeSet to another // using equals() method boolean value = set1.equals(set2); // print the value System.out.println("Are both set equal: " + value); } } |
First TreeSet: [10, 20, 30, 40, 50] Second TreeSet: [10, 20, 30] Are both set equal: false
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- TreeSet pollLast() method in Java with Example
- TreeSet toArray(T[]) method in Java with Example
- TreeSet toArray() method in Java with Example
- TreeSet toString() method in Java with Example
- TreeSet retainAll() method in Java with Example
- TreeSet removeAll() method in Java with Example
- TreeSet hashCode() method in Java with Example
- TreeSet containsAll() method in Java with Example
- Stack equals() method in Java with Example
- LinkedHashSet equals() method in Java with Example
- AbstractSequentialList equals() method in Java with Example
- HashSet equals() method in Java with Example
- LinkedBlockingDeque equals() method in Java with Example
- ConcurrentLinkedDeque equals() method in Java with Example
- StringWriter equals() method in Java with Example
- DecimalStyle equals() method in Java with Example
- Collator equals(String, String) method in Java with Example
- Collator equals(Object) method in Java with Example
- FieldPosition equals() method in Java with Example
- MessageFormat equals() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

