Java Arrays hashCode(boolean[]) 方法



描述

Java Arrays hashCode(boolean[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個布林型陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(boolean[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(byte[]) 方法

描述

Java Arrays hashCode(byte[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個位元組陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(byte[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(char[]) 方法

描述

Java Arrays hashCode(char[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個字元陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(char[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(double[]) 方法

描述

Java Arrays hashCode(double[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個雙精度浮點數陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(double[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(float[]) 方法

描述

Java Arrays hashCode(float[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個單精度浮點數陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(float[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(int[]) 方法

描述

Java Arrays hashCode(int[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個整數陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(int[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(long[]) 方法

描述

Java Arrays hashCode(long[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個長整數陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(long[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(short[]) 方法

描述

Java Arrays hashCode(short[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個短整數陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(short[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

Java Arrays hashCode(Object[]) 方法

描述

Java Arrays hashCode(Object[]) 方法根據指定陣列的內容返回一個雜湊碼。對於任意兩個物件陣列 a 和 b,如果 Arrays.equals(a, b) 成立,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也成立。

宣告

以下是 java.util.Arrays.hashCode() 方法的宣告

public static int hashCode(Object[] a)

引數

a − 這是要計算雜湊值的陣列。

返回值

此方法返回 a 的基於內容的雜湊碼。

異常

獲取布林型、位元組型、字元型、雙精度浮點數型陣列的雜湊碼示例

以下示例演示了 Java Arrays hashcode() 方法在 boolean[]、byte[]、char[] 和 double[] 上的用法。首先,我們建立了四個布林型、位元組型、字元型和雙精度浮點數型的陣列,然後使用 Arrays hashCode() 方法列印它們的雜湊碼。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing arrays
      boolean[] boolArr = new boolean[] { true, true };
      byte[] byteArr = new byte[] {10, 20 };
      char[] charArr = new char[] {'A', 'B' };
      double[] doubleArr = new double[] {10.0, 20.0 };

      // printing hash code value
      System.out.println("The hash code of boolean array is: " + Arrays.hashCode(boolArr));
      System.out.println("The hash code of byte array is: " + Arrays.hashCode(byteArr));
      System.out.println("The hash code of char array is: " + Arrays.hashCode(charArr));
      System.out.println("The hash code of double array is: " + Arrays.hashCode(doubleArr));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

The hash code of boolean array is: 40353
The hash code of byte array is: 1291
The hash code of char array is: 3042
The hash code of double array is: 76547009

獲取單精度浮點數型、整數型、長整數型和短整數型陣列的雜湊碼示例

以下示例演示了 Java Arrays hashcode() 方法在 float[]、int[]、long[] 和 short[] 上的用法。首先,我們建立了四個單精度浮點數型、整數型、長整數型和短整數型陣列,然後使用 Arrays hashCode() 方法列印它們的雜湊碼。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing arrays
      float[] floatArr = new float[] { 10.0f, 20.f };
      int[] intArr = new int[] {10, 20 };
      long[] longArr = new long[] { 10L, 20L };
      short[] shortArr = new short[] {10, 20 };

      // printing hash code value
      System.out.println("The hash code of float array is: " + Arrays.hashCode(floatArr));
      System.out.println("The hash code of int array is: " + Arrays.hashCode(intArr));
      System.out.println("The hash code of long array is: " + Arrays.hashCode(longArr));
      System.out.println("The hash code of short array is: " + Arrays.hashCode(shortArr));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

The hash code of float array is: 612369345
The hash code of int array is: 1291
The hash code of long array is: 1291
The hash code of short array is: 1291

獲取物件陣列的雜湊碼示例

以下示例演示了 Java Arrays hashcode() 方法在 Object[] 上的用法。首先,我們建立了一個 Student 物件陣列,然後使用 Arrays hashCode() 方法列印它們的雜湊碼。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing array
      Student[] students = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      // printing hash code value
      System.out.println("The hash code of Object array is: " + Arrays.hashCode(students));
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

The hash code of Object array is: -1497234753
java_util_arrays.htm
廣告