Java Arrays hashCode() Method
In this tutorial, we will learn about the hashCode()
method of the Arrays
class in Java. This method will convert the given array to the hash code based on the content of the array. There are many overloading methods of hashCode()
and here we will explore one by one
Syntax
This method returns a hash code based on the contents of the given array of boolean
type.
public static int hashCode(boolean[] arr)
List of the Overloading Methods of hashCode()
Method
This table contains all the overloaded variants of hashCode()
method.
Method
|
Description
|
static int hashCode(boolean[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(byte[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(char[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(double[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(float[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(int[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(long[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(short[] a)
|
This method returns a hash code based on the contents of the specified array.
|
static int hashCode(Object[] a)
|
This method returns a hash code based on the contents of the specified array.
|
Example: Get Hash Code of boolean Array
In the following example, we are passing an array of boolean
values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
boolean arr[] = {true, false, true, true, false};
int hashCode=Arrays.hashCode(arr);
System.out.println("Hash Code Of Array arr is: "+hashCode);
}
}
Hash Code Of Array arr is: 1203557358
Example: Get Hash Code of byte Array
In the following example, we are passing an array of byte
values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
byte arr[] = {7, 5, 12, 20, 30};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for byte array: "+hashCode);
}
}
HashCode for byte array: 35254935
Example: Get Hash Code of char Array
In the following example, we are passing an array of char
values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
char arr[] = {'A', 'B', 'C', 'D', 'E'};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for char array: "+hashCode);
}
}
HashCode for char array: 90690786
Example: Get Hash Code of double Array
In the following example, we are passing an array of double
values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
double arr[] = {3.2, 5.2, 11.3, 56.21};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for double array: "+hashCode);
}
}
HashCode for double array: 425363286
Example: Get Hash Code of float Array
In the following example, we are passing an array of float values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
float arr[] = {3.2f, 5.2f, 11.3f, 56.21f};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for float array: "+hashCode);
}
}
HashCode for float array: -2117663657
Example: Get Hash Code of int Array
In the following example, we are passing an array of int values to the hashCode() method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
int arr[] = {3, 2, 4, 5, 7};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for int array: "+hashCode);
}
}
HashCode for int array: 31463302
Example: Get Hash Code of long Array
In the following example, we are passing an array of long values to the hashCode() method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
long arr[] = {39846, 28784, 45464, 12315, 7418527};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for long array: "+hashCode);
}
}
HashCode for long array: -918463407
Example: Get Hash Code of short Array
In the following example, we are passing an array of short values to the hashCode() method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
short arr[] = {5, 5, 1, 4, 6};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for short array: "+hashCode);
}
}
HashCode for short array: 33396802
Example: Get Hash Code of Object Array
In the following example, we are passing an array of Object values to the hashCode()
method, and this method returns a hash code based on the content of the array in the integer format.
import java.util.Arrays;
class StudyTonight {
public static void main(String args[])
{
Object arr[] = {5, 5, 1, 4, 6};
int hashCode=Arrays.hashCode(arr);
System.out.println("HashCode for Object array: "+hashCode);
}
}
HashCode for Object array: 33396802
Conclusion:
In this tutorial, we learned how to generate a hash value of an array using the hashCode()
method. This method generates a hash value based on the content of an array. There are many overloading methods of this method to support an array of different data types.