Set hashCode() Method in Java Last Updated : 06 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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 HashSet. Java // Java Program to demonstrate the working of // hashCode() method in Set import java.util.*; public class Geeks { public static void main(String[] args) { // creating an Set Set<Integer> s = new HashSet<Integer>(); // using add() to insert elements s.add(1); s.add(2); s.add(3); s.add(4); System.out.println("Set: " + s); // Get the hashCode value // using hashCode() value System.out.println("HashCode value: " + s.hashCode()); } } OutputSet: [1, 2, 3, 4] HashCode value: 10 Syntax of hashCode() Methodpublic int hashCode()Parameter: This method does not take any parameter.Return Type: This method return an int value.Example 2: This example demonstrates that without overriding hashCode(), different objects with similar data have different hash codes. Java // Java program to demonstrates hashcode() // value for different objects class Geeks { int id; String name; public Geeks(int id, String name) { this.id = id; this.name = name; } public static void main(String[] args) { Geeks obj1 = new Geeks(1, "John"); Geeks obj2 = new Geeks(1, "John"); System.out.println("hashCode of obj1: " + obj1.hashCode()); System.out.println("hashCode of obj2: " + obj2.hashCode()); } } OutputhashCode of obj1: 1510467688 hashCode of obj2: 868693306 Comment More infoAdvertise with us Next Article Set iterator() method in Java with Examples gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-set Practice Tags : JavaJava-Collections Similar Reads Set in Java The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat 14 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 Set contains() method in Java with Examples The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme 2 min read Set remove() method in Java with Examples The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t 1 min read Set addAll() Method in Java In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order.Example 1: This example demonstrates how to merge two TreeSet using the addAll() method.Java// Java 2 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read Set containsAll() Method in Java The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are iden 2 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 Set iterator() method in Java with Examples The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i 1 min read Set removeAll() Method in Java In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection.Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second 2 min read Like