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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (48 Blogs)
  • Comprehensive Java Course (1 Blogs)
  • Java/J2EE and SOA (195 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Everything You Need To know About Java Hashcode

Published on Jul 18,2019 14 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.