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

AbstractSet hashCode() Method in Java with Examples

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

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 element is stored successfully. This is how the hash code works.

Syntax:

AbstractSet.hashCode()

Parameters: The method does not accept any parameters.

Return Value: The method returns the hashcode value of the set.

Below programs are used to illustrate the working of AbstractSet.hashCode() Method:

Program 1:




// Java code to illustrate the hashCode() method
  
import java.util.*;
  
public class Abstract_Set_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractSet
        AbstractSet<String>
            abs_set = new HashSet<String>();
  
        // Adding elements into the set
        abs_set.add("Geeks");
        abs_set.add("4");
        abs_set.add("Geeks");
        abs_set.add("Welcomes");
        abs_set.add("You");
  
        // Displaying the AbstractSet
        System.out.println("Initial Set is: "
                           + abs_set);
  
        // Getting the hashcode value for the set
        System.out.println("The hashcode value of the set: "
                           + abs_set.hashCode());
    }
}


Output:

Initial Set is: [4, Geeks, You, Welcomes]
The hashcode value of the set-295204749

Program 2:




// Java code to illustrate the hashCode() method
  
import java.util.*;
  
public class Abstract_Set_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty AbstractSet
        AbstractSet<Integer>
            abs_set = new TreeSet<Integer>();
  
        // Adding elements into the set
        abs_set.add(15);
        abs_set.add(20);
        abs_set.add(30);
        abs_set.add(40);
        abs_set.add(50);
  
        // Displaying the AbstractSet
        System.out.println("Initial Set is: "
                           + abs_set);
  
        // Getting the hashcode value for the set
        System.out.println("The hashcode value of the set: "
                           + abs_set.hashCode());
    }
}


Output:

Initial Set is: [15, 20, 30, 40, 50]
The hashcode value of the set: 155

Note: The same operation can be performed with any type of Set with variation and combination of different data types.



Previous Article
Next Article

Similar Reads

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 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
2 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