🌐
Baeldung
baeldung.com › home › java › core java › guide to hashcode() in java
Guide to hashCode() in Java | Baeldung
December 8, 2025 - In Java, efficient hashing algorithms stand behind some of the most popular collections, such as the HashMap (check out this in-depth article) and the HashSet. In this tutorial, we’ll focus on how hashCode() works, how it plays into collections and how to implement it correctly.
🌐
GeeksforGeeks
geeksforgeeks.org › java › method-class-hashcode-method-in-java
Method Class | hashCode() Method in Java - GeeksforGeeks
July 11, 2025 - Returns: It returns an integer value which represents hashCode value for this Method.
🌐
W3Schools
w3schools.com › java › ref_string_hashcode.asp
Java String hashCode() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... The hashCode() method returns the hash code of a string.
🌐
SitePoint
sitepoint.com › blog › java › how to implement java’s hashcode correctly
How to Implement Java's hashCode Correctly — SitePoint
November 7, 2024 - The hashCode() method in Java works by generating an integer value that represents the memory address of the object. This value is used as the index number for the object in the hash-based collection.
🌐
Medium
medium.com › @AlexanderObregon › what-is-hashcode-in-java-a-simple-guide-a3b95d6ebae4
What are Hashcodes in Java? A Simple Guide
April 2, 2024 - Explore the essentials of Java hashcodes with our guide. Learn to implement hashCode() for efficient data handling in your Java applications.
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_hashcode.htm
Java - String hashCode() Method
public int hashCode() Here is the detail of parameters − · This is a default method and this will not accept any parameters. This method returns a hash code value for this object. import java.io.*; public class Test { public static void main(String args[]) { String Str = new String("Welcome to Tutorialspoint.com"); System.out.println("Hashcode for Str :" + Str.hashCode() ); } } This will produce the following result − ·
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › Object › hashCode
Java Object hashCode() - Generate Hash Code | Vultr Docs
November 19, 2024 - In this article, you will learn how to effectively use the hashCode() method in Java. Explore how this method facilitates easier data manipulation and enhances performance when working with collections.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-equals-hashcode
Java equals() and hashCode() | DigitalOcean
August 3, 2022 - Implement hashCode() method so that least number of hash collision occurs and entries are evenly distributed across all the buckets. You can download the complete code from our GitHub Repository. Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, ...
🌐
CodeGym
codegym.cc › java blog › java objects › java hashcode()
What is Java hashcode
January 8, 2025 - If two objects are equal, their hashcodes are the same. The reverse is not true. If the hash codes are different, then the objects are not equal for sure. Different objects may have the same hash code. However, it is a very unlikely event. At this point, we have a collision, a situation, where we can lose data. The "proper" hash function minimizes the probability of collisions. In Java hash function is usually connected to hashCode() method.
Find elsewhere
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java hashcode()
Java hashCode()
September 30, 2022 - A hashcode() object value can change many/multiple executions which is of the same application. The hash code is nothing but an integer value that is associated with each of the objects in Java.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Javapractices
javapractices.com › topic › TopicAction.do
Java Practices->Implementing hashCode
import java.lang.reflect.Array; /** * Collected methods which allow easy implementation of <tt>hashCode</tt>. * * Example use case: * <pre> * public int hashCode(){ * int result = HashCodeUtil.SEED; * //collect the contributions of various fields * result = HashCodeUtil.hash(result, fPrimitive); * result = HashCodeUtil.hash(result, fObject); * result = HashCodeUtil.hash(result, fArray); * return result; * } * </pre> */ public final class HashCodeUtil { /** * An initial value for a <tt>hashCode</tt>, to which is added contributions * from fields.
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java object hashcode method
Java Object hashCode Method
September 1, 2008 - The following example shows the usage of java.lang.Object.hashCode() method. In this program, we've created a new instance of GregorianCalendar Class. Now using hashCode() method, the hash code of the calendar instance is printed. package com.tutorialspoint; import java.util.GregorianCalendar; public class ObjectDemo { public static void main(String[] args) { // create a new GregorianCalendar object GregorianCalendar cal = new GregorianCalendar(); // print current time System.out.println("" + cal.getTime()); // print a hashcode for cal System.out.println("" + cal.hashCode()); } }
Top answer
1 of 7
270

hashCode() is used for bucketing in Hash implementations like HashMap, HashTable, HashSet, etc.

The value received from hashCode() is used as the bucket number for storing elements of the set/map. This bucket number is the address of the element inside the set/map.

When you do contains() it will take the hash code of the element, then look for the bucket where hash code points to. If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals() method to evaluate if the objects are equal, and then decide if contains() is true or false, or decide if element could be added in the set or not.

2 of 7
41

From the Javadoc:

Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.

  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)

🌐
Scaler
scaler.com › home › topics › hashcode() in java
HashCode() in Java | Java hashcode Method - Scaler Topics
May 1, 2024 - Hashing is a Computer Science concept that maps an object or entity to an integer. The hash value of an object is an integer value that is computed using the properties of that object. Every object in Java inherits the hashCode() and equals() method.
🌐
Programiz
programiz.com › java-programming › library › object › hashcode
Java Object hashCode()
Hence, every class can implement the hashCode() method. import java.util.ArrayList; class Main { public static void main(String[] args) { // hashCode() with String String str = new String(); System.out.println(str.hashCode()); // 0 ArrayList<Integer> list = new ArrayList<>(); System.out.println(list.hashCode()); // 1 } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-hashcode-method-with-examples
Java String hashCode() Method with Examples - GeeksforGeeks
June 6, 2023 - The hashCode() method is the inherited method from the Object class in the String class that is used for returning the hash value of a particular value of the String type. ... This method doesn't take any parameters.
🌐
Medium
medium.com › @AlexanderObregon › java-hashcode-calculations-explained-a735eb2a0a5e
Java Hashcode Calculations Explained | Medium
June 19, 2024 - Learn how Java hashcode calculations work, why they're important for collections like HashMap and HashSet, and best practices for implementing hashCode.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Object.html
Object (Java Platform SE 7 )
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
🌐
LabEx
labex.io › tutorials › java-how-to-implement-hashcode-in-java-418030
How to implement hashCode in Java | LabEx
Understanding how to implement ... data structures. This comprehensive tutorial explores the essential techniques and best practices for creating robust and efficient hash code implementations in Java, helping .....
🌐
Studytonight
studytonight.com › java-examples › hashcode-method-in-java
hashCode() Method in Java - Studytonight
This tutorial explains the hashCode() method in Java and also explains how it is used in Hash Tables.