Introduction
This lab will cover the usage of the hashCode(int n)
method of the Java Integer
class. We will learn how to generate a unique integer value, which is known as a hash code, for a given integer value.
This lab will cover the usage of the hashCode(int n)
method of the Java Integer
class. We will learn how to generate a unique integer value, which is known as a hash code, for a given integer value.
Create a file named IntegerHashcode.java
in the ~/project
directory and open it in your favorite text editor.
touch ~/project/IntegerHashcode.java
touch ~/project/IntegerHashcode.java
Import the Integer
class from the java.lang
package.
import java.lang.Integer;
In this step, we will generate hash codes for integer values using the hashCode(int n)
method. We will create an application that prompts the user to enter an integer value and then generates the hash code value for the entered integer.
import java.lang.Integer;
import java.util.Scanner;
public class IntegerHashcode {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer value: ");
int value = sc.nextInt();
int hashValue = Integer.hashCode(value);
System.out.println("Hash code value for " + value + " is " + hashValue);
}
}
In the above code, we have imported the Scanner
class from the java.util
package for input. We prompt the user to enter an integer value and read it using the nextInt()
method of the Scanner
class. We then generate the hash code value for the entered integer using the hashCode(int n)
method and print it on the console using the println()
method.
Save and close the file. Compile the IntegerHashcode.java
file using the below command.
javac IntegerHashcode.java
Run the program using the below command.
java IntegerHashcode
You will be prompted to enter an integer value. Enter any integer value and press the enter key. The program will generate the hash code value for the entered integer value and display it on the console.
Let's modify the application to generate hash codes for multiple integer values entered by the user.
import java.lang.Integer;
import java.util.Arrays;
import java.util.Scanner;
public class IntegerHashcode {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of integer values: ");
int count = sc.nextInt();
int[] values = new int[count];
for (int i = 0; i < count; i++) {
System.out.print("Enter integer value " + (i+1) + ": ");
values[i] = sc.nextInt();
}
int[] hashValues = new int[count];
for (int i = 0; i < count; i++) {
hashValues[i] = Integer.hashCode(values[i]);
}
System.out.println("Entered integer values: " + Arrays.toString(values));
System.out.println("Corresponding hash code values: " + Arrays.toString(hashValues));
}
}
In the above code, we have added a loop to read multiple integer values from the user and generate hash code values for them. We store the integer values and hash code values in separate arrays and print them on the console using the toString()
method of the Arrays
class.
Save and close the file. Compile the IntegerHashcode.java
file again using the below command.
javac IntegerHashcode.java
Run the modified program using the below command.
java IntegerHashcode
Follow the prompts to enter multiple integer values. The program will generate hash code values for each integer value and display them on the console.
A hash code collision occurs when multiple objects generate the same hash code value. Although the hashCode(int n)
method generates unique hash code values for any given integer value, it is not foolproof. There is still a possibility of hash code collisions, especially when the hash code values and the underlying algorithm generating the hash codes are not strong enough.
Let's demonstrate a hash code collision using the hashCode(int n)
method. Modify the application by generating hash code values for a set of predefined integer values as shown below.
import java.lang.Integer;
import java.util.Arrays;
public class IntegerHashcode {
public static void main(String[] args) {
int[] values = {100, 200, 300, 400, 500};
int[] hashValues = new int[values.length];
for (int i = 0; i < values.length; i++) {
hashValues[i] = Integer.hashCode(values[i]);
}
System.out.println("Input integer values: " + Arrays.toString(values));
System.out.println("Corresponding hash code values: " + Arrays.toString(hashValues));
}
}
In the above code, we have predefined an array of integer values and generate hash code values for each of them. We then print both the input values and their corresponding hash code values on the console.
Save and close the file. Compile the IntegerHashcode.java
file one final time using the below command.
javac IntegerHashcode.java
Run the final program using the below command.
java IntegerHashcode
You will notice that the hash code values of 100
and 200
are the same. This is a hash code collision. Although hash code collisions are rare, they can potentially cause issues with hash-based collections in Java.
In this lab, we learned about the hashCode(int n)
method of the Java Integer
class. We learned how to generate unique hash code values for integer values using this method. We also learned how to handle hash code collisions and the limitations of hash codes in Java.