Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Everything You Need To know About Java Hashcode

Published on Jul 18,2019 1.5K Views


HashMaps and HashSets are used to manipulate data and this is done with the help of hashing. Above approaches use hashCode() method to check and verify hash values. Implementing hashCode() in Object class yields distinct integers for different objects. There are times we may be required to implement hashCode method in our program. In this article we would understand Java hashcode in detail,

This article focuses on following pointers,

So let us get started with the first topic of Java Hashcode article,

What Is Java HashCode?

It returns the hashcode value as an Integer. Hashcode value is mostly used in hashing based collections like HashMap, HashSet, HashTable….etc. This method must be overridden in every class which overrides equals() method.

 The general contract of hashCode() method is:

  • Multiple invocations of hashCode() should return the same integer value, unless the object property is modified that is being used in the equals() method.
  • An object hash code value can change in multiple executions of the same application.
  • If two objects are equal according to equals() method, then their hash code must be same.
  • If two objects are unequal according to equals() method, their hash code are not required to be different. Their hash code value may or may-not be equal.

Let us take a look at a sample code to understand the concept better, however I would suggest starting with Java Installation,

Sample Code For Java Hashcode

public int hashCode()

// This method returns the hash code value 

// for the object on which this method is invoked.

Example

public class Employee {
protected long employeeId;
protected String firstName;
protected String lastName;
public int hashCode(){
return(int)employeeId;
}
}

Notice, that if two Employee objects are equal, they will also have the same hash code. But, as is especially easy to see in the example, two Employee objects can be not equal, and still have the same hash code. There you go, we have successfully explored the concept.

Thus we have come to an end of this article on ‘Java Hashcode’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need To know About Java Hashcode

edureka.co