HashSet equals() method in Java with Example
The equals() method of java.util.HashSet class is used verify the equality of an Object with a HashSet and compare them. The list returns true only if both HashSet contains same elements, irrespective of order.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
First HashSet: [A, B, C, D, E] Second HashSet: [A, B, C, D, E] Are both set equal: true
First HashSet: [50, 20, 40, 10, 30] Second HashSet: [20, 10, 30] Are both set equal: false