VOOZH about

URL: https://www.geeksforgeeks.org/java/map-hashcode-method-in-java-with-examples/

⇱ Java Map hashCode() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Map hashCode() Method

Last Updated : 11 Jul, 2025

In Java, the hashCode() method is a part of the Object class and is used to generate a hash code value for an object.

Example 1: Hash Code for Different Objects

The below Java program demonstrates that every object has a unique hashcode.


Output
HashCode of obj1: 1510467688
HashCode of obj2: 868693306

Note: The hashCode() method generates a hash code based on the objects's memory address or internal data.

Syntax of hashCode() Method

public int hashCode()

Return Type: This method returns an integer value that represent the hash code of the object.

Example 2: This example demonstrates storing key-value pair in a HashMap and retrieving its hash code.


Output
HashMap: {1=Geek1, 2=Geek2, 3=Geek3}
HashCode of the HashMap: 206037924

Explanation: The hash code of a HashMap is calculated based on the hash codes of its key-value pairs. This ensures that two HashMap objects with identical contents will have the same hash code.

Comment