A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/abstractset-hashcode-method-in-java-with-examples/ below:

Java AbstractSet hashCode() Method - GeeksforGeeks

Java AbstractSet hashCode() Method

Last Updated : 11 Jul, 2025

The hashCode() method in AbstractSet (and its subclasses like HashSet) computes a hash code based on the elements of the set. It ensures that if two sets contain the same elements, their hash codes will be the same, provided the elements themselves return consistent hash codes.

Working of Hash Code:

Example 1: This example demonstrates how to create a HashSet, add elements to it, and display its hash code.

Java
// Java program to display the hashcode of a set
import java.util.HashSet;
import java.util.Set;

public class Geeks {
  
    public static void main(String[] args)
    {
        // Create three Set
        Set<Integer> s1 = new HashSet<>();
        s1.add(1);
        s1.add(2);
        s1.add(3);

        System.out.println("Set1: " + s1);
  
        // Displaying hash code
        System.out.println("Hash code of set1: "
                           + s1.hashCode()); 
    }
}

Output
Set1: [1, 2, 3]
Hash code of set1: 6

Explanation: AbstractSet sums the hash codes of its elements:

hashCode = 1.hashCode() + 2.hashCode() + 3.hashCode()

= 1 + 2 + 3

= 6

Syntax of hashCode() Method

public int hashCode()

Example 2: This example demonstrates how hashCode and equals() work together to compare custom objects correctly and ensure they have same hash codes based on their properties.

Java
// Java program to demonstrates how hascode() works with
// custom objects

import java.util.HashSet;
import java.util.Objects;

class Person {
    String name;
    int age;

    Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    // Override hashCode()
    @Override public int hashCode()
    {
        // Generates a hash code based on name and age
        return Objects.hash(name, age);
    }

    // Override equals() to ensure consistency
    @Override public boolean equals(Object o)
    {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        Person person = (Person)o;
        return age == person.age
            && Objects.equals(name, person.name);
    }
}

public class HashCodeDemo {
    public static void main(String[] args)
    {
        Person p1 = new Person("Alice", 30);
        Person p2 = new Person("Alice", 30);
        Person p3 = new Person("Bob", 25);

        // Print hash codes
        System.out.println("Hash code of p1: "
                           + p1.hashCode());
        System.out.println("Hash code of p2: "
                           + p2.hashCode());
        System.out.println("Hash code of p3: "
                           + p3.hashCode());

        // Check equality
        System.out.println("p1 equals p2: "
                           + p1.equals(p2));
        System.out.println("p1 equals p3: "
                           + p1.equals(p3));
    }
}

Output
Hash code of p1: 1963862399
Hash code of p2: 1963862399
Hash code of p3: 2076901
p1 equals p2: true
p1 equals p3: false


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4