Set equals() method in Java with Examples

The equals() method of java.util.Set class is used verify the equality of an Object with a Set and compare them. The method returns true only if both Set contains same elements with 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate equals()
// method of Set
  
import java.util.*;
  
public class GFG {
    public static void main(String[] argv)
    {
  
        // Creating object of Set
        Set<String> arrset1 = new HashSet<String>();
  
        // Populating arrset1
        arrset1.add("A");
        arrset1.add("B");
        arrset1.add("C");
        arrset1.add("D");
        arrset1.add("E");
  
        // print arrset1
        System.out.println("First Set: "
                           + arrset1);
  
        // Creating another object of Set
        Set<String> arrset2 = new HashSet<String>();
  
        // Populating arrset2
        arrset2.add("A");
        arrset2.add("B");
        arrset2.add("C");
        arrset2.add("D");
        arrset2.add("E");
  
        // print arrset2
        System.out.println("Second Set: "
                           + arrset2);
  
        // comparing first Set to another
        // using equals() method
        boolean value = arrset1.equals(arrset2);
  
        // print the value
        System.out.println("Are both set equal? "
                           + value);
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate equals()
// method of Set
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
    {
  
        // Creating object of Set
        Set<Integer> arrset1 = new HashSet<Integer>();
  
        // Populating arrset1
        arrset1.add(10);
        arrset1.add(20);
        arrset1.add(30);
        arrset1.add(40);
        arrset1.add(50);
  
        // print arrset1
        System.out.println("First Set: " + arrset1);
  
        // Creating another object of Set
        Set<Integer> arrset2 = new HashSet<Integer>();
  
        // Populating arrset2
        arrset2.add(10);
        arrset2.add(20);
        arrset2.add(30);
  
        // print arrset2
        System.out.println("Second Set: " + arrset2);
  
        // comparing first Set to another
        // using equals() method
        boolean value = arrset1.equals(arrset2);
  
        // print the value
        System.out.println("Are both set equal? "
                           + value);
    }
}

chevron_right


Output:

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

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#equals(java.lang.Object)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.