Introduction to Java hashCode()
The hashcode() method of the Java Programming Language is always present in the Object Class. So for every Java Programming class will get the default implementation of the hashcode() method. This hashcode() method is an integer hashcode value of the object, and it is a native method. The multiple/many invocations the hashcode() method have to return the same integer value, but it will be done only when the object is modified, which is used in the equals() method too. 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.
Syntax:
public int hashcode()
How does the hashCode() method work in Java?
The hashcode() Method works in java by returning some hashcode value just as an Integer. This hashcode integer value is vastly used in some hashing based collections, which are like HashMap, HashTable, HashSet, etc. The hashcode() method of Java is needed to be overridden in every class, which helps to override the methods like equal().
During the time of executing the application, If the hashcode() method is invoked more than one time on the particular same object, then hashcode() will return the same integer value consistently. The integer value will remain the same from only one execution of the particular application to some other execution of the same app/application. If the 2 objects are equal, then the hashcode() method of Java coding language will produce the same integer and on each of the 2 objects, if these two objects are not equal/unequal, then producing the integer value which is going to be produced by the hashcode() method on one of the two objects which will be distinct. It is going to be similar but producing different integer value on each of all the two objects is better/best for improving the hashing performance-based collections, which are like HashTable, HashMap.. etc.
Equal/Similar objects will produce the same hash code when the objects are equal up to the final extent. Unequal objects of hashcode() don’t produce some distinct/different hash codes.
Examples to Implement Java hashCode()
Below are the examples mentioned:
Example #1
This is an example of converting a blog link and some text into hash code conversion. At first, a public class “StringExample1” is created. Before this, save the program file in the name of StringExample1.java for this program. Then the main() function is created to enter the java program code. Then a string variable called “blogName1” is created with the value “ profitloops.com ”. Then again, a textprint1 variable is created with some string text.
Then hashcode() function is made to convert the profitloops.com into hashcode. Likewise, for the other string text too, the string will be converted into hash code. To print the hash codes of those string texts, the “System.out.println()” function is used. Then closing of the parenthesis for the correct syntax purpose. Check out the output below so that you will get an idea of how the hash code is turned.
Code:
public class StringExample1{
public static void main(String[] args)
{
String blogName1 = "profitloops.com";
String textprint1 = "This is the Hashcode of the 'profitloops.com :: '";
System.out.println(textprint1);
System.out.println( blogName1.hashCode() );
System.out.println("This is the Hashcode of the string 'EDUCBA :: '");
System.out.println( "EDUCBA".hashCode() );
}
}
Output:
Example #2
This is the example of hashcode() on various types of characters, texts, Null Value, etc.. to know how the hashcode() turned and to know what will be the result of the specific input when hashcode() is used. At first, a public class “StringExample1” is created, and the public main is also created to enter the program code. Then hashcode() is used to know what are the hash codes for the NULL element, Space element. Here strings called “EDUCBA”, “physics” and “PHYSICS”, characters small alphabet “a”, big alphabet “b” is converted into hash codes with the help of the hashcode() function. System.out.println() function is used to show the terminal or command prompt’s output or any other to show the hash code value for different types of string values or any other values.
Code:
public class StringExample1{
public static void main(String[] args)
{
String blogName1 = "";
System.out.println("This is the Hashcode of the Null Element::");
System.out.println( "".hashCode() );
System.out.println("This is the HashCode of Space Element::");
System.out.println( " ".hashCode() );
System.out.println("This is the Hashcode of the string 'EDUCBA :: '");
System.out.println( "EDUCBA".hashCode() );
System.out.println("This is the HashCode of the alphabet small a::");
System.out.println( "a".hashCode() );
System.out.println("This is the HashCode of the alphabet big A::");
System.out.println( "A".hashCode() );
System.out.println("This is the HashCode of the word physics::");
System.out.println( "physics".hashCode() );
System.out.println("This is the HashCode of the word PHYSICS::");
System.out.println( "PHYSICS".hashCode() );
}
}
Output:
Example #3
This is an example of implementing the hash codes for different variable values. If two variables are equal, those will provide an output as equal variables and then hash codes for those. Likewise, if two variable values are not equal, then under unequal variables, those variables’ hash codes will also be printed.
At first, public class “hash1” is created, and the public main is created. Then two variables, a1 and b1, are created with the same values. Then those two variable values are checked. If same, a1 and b1 hashcode values are printed. Here a1 and b1 are the same. Then string values c1 and d1 are created with different string values like 10 and 50. String values c1 and d1 are checked to know whether they are equal or not. Here not equal so “Unequal variables:” text is printed, and then hash codes of those variable values are printed. Check out the output of this hashcode() example so that you will understand the hashcode() concept better.
Code:
public class hash1{
public static void main(String[] args){
String a1 = "200";
String b1 = "200";
if(a1.equals(b1)){
System.out.println("Equal variables: \n");
System.out.println("A1 variable 200's hashcode :: "+a1.hashCode() + "\n B1 variable value 200's hashcode :: " + b1.hashCode()+"\n");
}
String c1 = "10";
String d1 = "50";
if(!c1.equals(d1)){
System.out.println("\nUn-equal variables: \n");
System.out.println("C1 variable value 10's hash code :: "+c1.hashCode() + "\nD1 variable value 50's hashcode :: " + d1.hashCode()+"\n");
}
}
}
Output:
Conclusion
We hope you learned what the definition of the Java hashcode() method and its syntax and its explanation is, How the hashcode() method of the Java Programming Language works with various examples to understand the concept better and so easily.
Recommended Articles
This is a guide to Java hashCode(). Here we discuss an introduction to Java hashCode() with syntax, how does it works and examples to implement. You can also go through our other related articles to learn more –