Java Arrays.hashCode()方法

原文:https://www.studytonight.com/java-util/java-arrays-hashcode-method

在本教程中,我们将学习 JavaArrays类的hashCode()方法。此方法将根据数组的内容将给定数组转换为哈希代码。hashCode() 的重载方法有很多,这里我们将一一探讨

句法

该方法根据给定的boolean 类型数组的内容返回一个哈希代码。

public static int hashCode(boolean[] arr)

hashCode()方法重载方法列表

此表包含hashCode()方法的所有重载变体。

|

方法

|

描述

| | --- | --- | | 静态 int hashCode(布尔[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(字节[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(char[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(double[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(float[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(int[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(long[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(short[] a) | 此方法基于指定数组的内容返回哈希代码。 | | 静态 int hashCode(Object[] a) | 此方法基于指定数组的内容返回哈希代码。 |

示例:获取布尔数组的哈希代码

在下面的例子中,我们将boolean 值的数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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);
    } 
}

数组 arr 的哈希码为:1203557358

示例:获取字节数组的哈希代码

在下面的例子中,我们将byte 值的数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:35254935

示例:获取字符数组的哈希代码

在下面的例子中,我们将char 值的数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:90690786

示例:获取双数组的哈希代码

在下面的例子中,我们将double 值的数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:425363286

示例:获取浮点数组的哈希代码

在下面的例子中,我们将一个浮点值数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:-2117663657

示例:获取整型数组的哈希代码

在下面的示例中,我们将一个 int 值数组传递给 hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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);
    } 
}

int 数组的 HashCode:31463302

示例:获取长数组的哈希代码

在下面的示例中,我们将一个长值数组传递给 hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:-918463407

示例:获取短数组的哈希代码

在下面的示例中,我们将一个短值数组传递给 hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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:33396802

示例:获取对象数组的哈希代码

在下面的例子中,我们将一个 Object 值数组传递给hashCode()方法,该方法以整数格式返回基于数组内容的哈希代码。

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);
    } 
}

对象数组的哈希码:33396802

结论:

在本教程中,我们学习了如何使用hashCode() 方法生成数组的哈希值。此方法基于数组的内容生成哈希值。此方法有许多重载方法来支持不同数据类型的数组。