AbstractSet equals() Method in Java with Examples
The AbstractSet.equals() method in Java AbstractSet is used to check for equality between two sets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not.
Syntax:
AbstractSet1.equals(AbstractSet2)
Parameters: The method accepts one parameter AbstractSet2 of abstract set type and refers to the set whose equality is to be checked with this abstract set.
Return Value: The method returns true if the equality holds for both the object set else it returns false.
Below programs illustrate the working of AbstractSet.equals() method:
Program 1:
// Java code to illustrate the equals() method import java.util.*; public class Abstract_Set_Demo { public static void main(String[] args) { // Creating an empty AbstractSet AbstractSet<String> abstract_set1 = new HashSet<String>(); AbstractSet<String> abstract_set2 = new HashSet<String>(); // Adding elements to set abstract_set1.add("Geeks"); abstract_set1.add("4"); abstract_set1.add("Geeks"); abstract_set1.add("Welcomes"); abstract_set1.add("You"); // Adding elements to set abstract_set2.add("Geeks"); abstract_set2.add("4"); abstract_set2.add("Geeks"); abstract_set2.add("Welcomes"); abstract_set2.add("You"); // Displaying the first HashSet System.out.println("First Set: " + abstract_set1); // Displaying the second HashSet System.out.println("Second Set: " + abstract_set2); // Displaying the equality System.out.println("Equality: " + abstract_set1 .equals(abstract_set2)); } } |
First Set: [4, Geeks, You, Welcomes] Second Set: [4, Geeks, You, Welcomes] Equality: true
Program 2:
// Java code to illustrate the equals() method import java.util.*; public class Abstract_Set_Demo { public static void main(String[] args) { // Creating an empty AbstractSet AbstractSet<String> abstract_set1 = new HashSet<String>(); AbstractSet<String> abstract_set2 = new HashSet<String>(); // Adding elements to set abstract_set1.add("Geeks"); abstract_set1.add("4"); abstract_set1.add("Geeks"); abstract_set1.add("Welcomes"); abstract_set1.add("You"); // Adding elements to set abstract_set2.add("Geeks"); abstract_set2.add("4"); abstract_set2.add("Geeks"); abstract_set2.add("Welcomes"); abstract_set2.add("U"); // Displaying the first HashSet System.out.println("First Set: " + abstract_set1); // Displaying the second HashSet System.out.println("Second Set: " + abstract_set2); // Displaying the equality System.out.println("Equality: " + abstract_set1 .equals(abstract_set2)); } } |
First Set: [4, Geeks, You, Welcomes] Second Set: [4, U, Geeks, Welcomes] Equality: false
Recommended Posts:
- AbstractSet hashCode() Method in Java with Examples
- AbstractSet removeAll() Method in Java with Examples
- AbstractSet Class in Java with Examples
- Map equals() method in Java with Examples
- Set equals() method in Java with Examples
- Optional equals() method in Java with Examples
- FloatBuffer equals() method in Java with Examples
- Date equals() method in Java with Examples
- AbstractList equals() method in Java with Examples
- Java 8 Clock equals() Method with Examples
- DateFormat equals() Method in Java with Examples
- OptionalLong equals() method in Java with examples
- Byte equals() method in Java with examples
- ChronoPeriod equals() method in Java with Examples
- OptionalInt equals() method in Java with examples
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.



