0% found this document useful (0 votes)
4 views2 pages

Java AbstractMap HashCode Method - GeeksforGeeks

The document explains the hashCode() method of the AbstractMap class in Java, which calculates a hash code value based on the key-value mappings in a map. It provides examples demonstrating the hash code of both a non-empty and an empty HashMap, highlighting that the hash code for an empty map is 0. The document also includes a suggested quiz question related to adding null keys in a HashMap.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java AbstractMap HashCode Method - GeeksforGeeks

The document explains the hashCode() method of the AbstractMap class in Java, which calculates a hash code value based on the key-value mappings in a map. It provides examples demonstrating the hash code of both a non-empty and an empty HashMap, highlighting that the hash code for an empty map is 0. The document also includes a suggested quiz question related to adding null keys in a HashMap.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Search...

Tutorials
Practice J
Jobs
Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Colle

Java AbstractMap hashCode() Method


Last Updated : 11 Jul, 2025

The hashCode() method of the AbstractMap class in Java is used to calculate a hash code value
for a map. This hash code is based on the key-value mapping contained in the map. It ensures
consistency such that two maps with the same entries have the same hash code.
Example 1: Hash Code of a Non-Empty Map

// Java program demonstrate the working of hashCode()


import [Link];
import [Link];
import [Link];

public class Geeks {


public static void main(String[] args)
{
// Create a HashMap
Map<String, Integer> hm = new HashMap<>();

// Add key-value pairs


[Link]("Java", 1);
[Link]("Programnming", 2);
[Link]("Language", 3);

// Get hashCode
[Link]("Map: " + hm);
[Link]("HashCode: " + [Link]());
}
}

Output

Map: {Programnming=2, Java=1, Language=3}


HashCode: -95421541

Explanation: In the above example, it creates a HashMap and adds three key-value pairs to it.
The hashCode() method calculates a unique integer value based on the keys and values in the
map.

Syntax of AbstractMap hashCode() Method


public int hashCode()

Return Type: It returns an integer representing the hash code value of the map.
Example 2: Hash Code of an Empty Map

// Java programm to demonstrates the


// hash code of an empty HashMap
import [Link];
import [Link];
import [Link];

public class Geeks {


public static void main(String[] args)
{

// creating an empty HashMap


Map<String, Integer> hm1 = new HashMap<>();
[Link]("Empty Map: " + hm1);
[Link]("HashCode of Empty Map: "
+ [Link]());
}
}

Output

Empty Map: {}
HashCode of Empty Map: 0

Explanation: The above program creates an empty HashMap without any key-value pairs. The
hashCode() method returns 0 because no entries exist in the map.

Suggested Quiz 4 Questions

What will happen if you try to add a null key in a HashMap?

A Throws NullPointerException

B Allowed only if the value is non-null

C Allowed, but only once

D Allowed multiple times

View Explanation 1/4 < Previous Next >

Comment K chinm…

You might also like