java中的hashCode(obj[] a)方法有什麼作用?


java.util.Arrays 類的 hashCode(Object[]) 方法基於指定陣列的內容返回一個雜湊碼。如果該陣列包含其他元素陣列,那麼雜湊碼是基於它們的身份而不是內容。對於任何兩個陣列 a 和 b,使得 Arrays.equals(a, b) 為 true,那麼 Arrays.hashCode(a) == Arrays.hashCode(b) 也為 true。

示例

import java.util.Arrays;
   public class ArrayDemo {   
      public static void main(String[] args) {
      Object[] ob = new Object[] { 22, 7 };
      int retval = ob.hashCode();
      System.out.println("The hash code of value1 is: " + retval);
      
      ob = new Object[] { 3.5, 8.5 };
      retval = ob.hashCode();
      System.out.println("The hash code of value2 is: " + retval);
   }
}

輸出

The hash code of value1 is: 4072869
The hash code of value2 is: 1671711

更新於: 30-Jul-2019

99 瀏覽量

開啟你的事業

完成課程即可獲得認證

開始吧
廣告
© . All rights reserved.