VOOZH about

URL: https://www.geeksforgeeks.org/java/set-equals-method-in-java-with-examples/

⇱ Set equals() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Set equals() Method in Java

Last Updated : 11 Jul, 2025

In Java, the equals() method of the Set class is used to compare two sets for equality. It checks if two sets contain the same elements regardless of their order.

Example 1: This example demonstrates how to compare two HashSet collections for equality of type string using the equals() method.


Output
First Set: [A, B, C, D, E]
Second Set: [A, B, C, D, E]
Are both set equal? true

Syntax of equals() Method

boolean equals(Object o)

  • Parameter: This method takes an object as a parameter.
  • Return Type: This method returns "true" if the two sets are equal, otherwise it returns "false".

Example 2: This example demonstrates how to compare two HashSet collections for equality of type integer using the equals() method.


Output
First Set: [50, 20, 40, 10, 30]
Second Set: [20, 10, 30]
Are both set equal? false
Comment