Set equals() Method in Java Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Java // Java program to demonstrate // the working of equals() method import java.util.*; public class Geeks { public static void main(String[] argv) { // Creating object of Set Set<String> s1 = new HashSet<String>(); s1.add("A"); s1.add("B"); s1.add("C"); s1.add("D"); s1.add("E"); System.out.println("First Set: " + s1); // Creating another object of Set Set<String> s2 = new HashSet<String>(); s2.add("A"); s2.add("B"); s2.add("C"); s2.add("D"); s2.add("E"); System.out.println("Second Set: " + s2); // comparing first Set to another // using equals() method boolean value = s1.equals(s2); System.out.println("Are both set equal? " + value); } } OutputFirst Set: [A, B, C, D, E] Second Set: [A, B, C, D, E] Are both set equal? true Syntax of equals() Methodboolean 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. Java // Java Program to demonstrates // the working of equals() method import java.util.*; public class Geeks { public static void main(String[] argv) { // Creating object of Set Set<Integer> s1 = new HashSet<Integer>(); s1.add(10); s1.add(20); s1.add(30); s1.add(40); s1.add(50); System.out.println("First Set: " + s1); // Creating another object of Set Set<Integer> s2 = new HashSet<Integer>(); s2.add(10); s2.add(20); s2.add(30); System.out.println("Second Set: " + s2); // comparing first Set to another // using equals() method boolean value = s1.equals(s2); System.out.println("Are both set equal? " + value); } } OutputFirst Set: [50, 20, 40, 10, 30] Second Set: [20, 10, 30] Are both set equal? false Comment More infoAdvertise with us Next Article Java ArrayList set() Method G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Practice Tags : JavaJava-Collections Similar Reads Array set() method in Java The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value) Parameter : array : This is an array of type Object which is to be updated. index : This is the 3 min read Set hashCode() Method in Java In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe 2 min read Array setInt() method in Java The java.lang.reflect.Array.setInt() is an inbuilt method in Java and is used to set a specified int value to a specified index of a given object array. Syntax: Array.setInt(Object []array, int index, int value) Parameter: array: This is an array of type Object which is to be updated. index: This is 3 min read Java ArrayList set() Method The set() method of the ArrayList class in Java is used to replace an element at a specified position with a new value. This is very useful when we need to update an existing element in an ArrayList while maintaining the list's structure.Example 1: Here, we will use the set() method to update an ele 2 min read EnumSet of() Method in Java The java.util.EnumSet.of(E ele1, E ele2, E ele3, ...) method in Java is used to create an enum set initially containing the specified elements in the parameters. When multiple items are added at the same time the elements are pushed down the set as the new elements are added. When different elements 5 min read Set add() method in Java with Examples The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodbo 2 min read Like