The Wayback Machine - https://web.archive.org/web/20240618020222/https://www.geeksforgeeks.org/abstractset-equals-method-in-java-with-examples/
Open In App

AbstractSet equals() Method in Java with Examples

Last Updated : 26 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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));
    }
}


Output:

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));
    }
}


Output:

First Set: [4, Geeks, You, Welcomes]
Second Set: [4, U, Geeks, Welcomes]
Equality: false


Previous Article
Next Article

Similar Reads

AbstractSet hashCode() Method in Java with Examples
The AbstractSet.hashCode() method in Java AbstractSet is used to fetch the hash code value of a particular this AbstractSet. A set consists of a number of buckets to store the elements. Each bucket has a unique identity and when an element is inserted into a bucket, its hashcode is matched with the identifier of the bucket, and if its a match the e
2 min read
AbstractSet removeAll() Method in Java with Examples
The removeAll() method of Java AbstractSet class is used to remove from this set all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from this set. Returns Value: This method returns true if this
3 min read
AbstractSet Class in Java with Examples
AbstractSet class in Java is a part of the Java Collection Framework which implements the Collection interface and extends the AbstractCollection class. It provides a skeletal implementation of the Set interface. This class does not override any of the implementations from the AbstractCollection class, but merely adds implementations for the equals
9 min read
AbstractSet containsAll() method in Java with Example
The containsAll() method of Java AbstractSet is used to check whether two sets contain the same elements or not. It takes one set as a parameter and returns True if all of the elements of this set is present in the other set. Syntax: public boolean containsAll(Collection C) Parameters: The parameter C is a Collection. This parameter refers to the s
2 min read
AbstractSet isEmpty() method in Java with Example
The isEmpty() method of AbstractSet in Java is used to check whether this AbstractSet is empty or not. It returns an boolean value stating the same. Syntax: public boolean isEmpty() Parameters: This function has no parameters. Returns: The method returns an boolean value which states that this instance of the AbstractSet is empty or not. Below exam
2 min read
AbstractSet add() method in Java with Example
The add(E) method of AbstractSet Class appends the specified element to the end of this AbstractSet. Syntax: boolean add(E element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the AbstractSet. Return Value: This method returns True after suc
2 min read
AbstractSet retainAll() method in Java with Example
The retainAll() method of java.util.AbstractSet class is used to retain from this set all of its elements that are contained in the specified collection. Syntax: public boolean retainAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be retained from this set. Returns Value: This method returns true i
3 min read
AbstractSet size() method in Java with Example
The size() method of AbstractSet in Java is used to get the size for this instance of the AbstractSet. It returns an integer value which is the size for this instance of the AbstractSet. Syntax: public int size() Parameters: This function has no parameters. Returns: The method returns an integer value which is the size for this instance of the Abst
2 min read
AbstractSet toArray() method in Java with Example
The toArray() method of Java AbstractSet is used to form an array of the same elements as that of the AbstractSet. Basically, it copies all the element from a AbstractSet to a new array. Syntax: Object[] arr = AbstractSet.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read
AbstractSet toString() method in Java with Example
The toString() method of Java AbstractSet is used to return a string representation of the elements of the Collection. The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is used mainly to display collections other than String
2 min read