deepHashCode与字节数组

时间:2011-01-12 17:26:58

标签: java arrays hashcode

由于某些原因,Arrays.deepHashCode()无法与byte[]一起使用 还有其他等价物吗?

3 个答案:

答案 0 :(得分:7)

首先,不需要“深度”。这是一个原始的。你不需要Deep。

只需使用Arrays.hashCode(byte[] yourArray)

即可

编辑:为了澄清,Deep意味着深入研究数组中包含的对象。鉴于您正在处理基元,您只需在计算中使用原始值本身。这就是为什么Deep方法都不围绕基元的原因。

答案 1 :(得分:5)

接受的答案是正确的:使用 Arrays.hashCode 为byte []提供具有相同值的相同结果。 如果您有嵌套(深层)结构,则需要 Arrays.deepHashCode

import java.util.Arrays;

public class A {

    public static void main(String[] args) {
        byte[] a = {10, 32, -43, 80};
        byte[] b = {13, -40};
        byte[] c = {10, 32, -43, 80};

        System.out.println("NOTE: A and C have identical values, B differs");
        System.out.println("Using byte[].hashCode(): A and C have different hash codes");
        System.out.println("a = " + a.hashCode());
        System.out.println("b = " + b.hashCode());
        System.out.println("c = " + c.hashCode());

        System.out.println("Using Arrays.hashCode(): A and C have identical hash codes");
        System.out.println("a = " + Arrays.hashCode(a));
        System.out.println("b = " + Arrays.hashCode(b));
        System.out.println("c = " + Arrays.hashCode(c));

        System.out.println("Using Arrays.deepHashCode(): A and C have identical hash codes");
        System.out.println("a = " + Arrays.deepHashCode(new Object[]{a}));
        System.out.println("b = " + Arrays.deepHashCode(new Object[]{b}));
        System.out.println("c = " + Arrays.deepHashCode(new Object[]{c}));
    }
}

这导致输出:

NOTE: A and C have identical values, B differs
Using byte[].hashCode(): A and C have different hash codes
a = 141847843
b = 329849131
c = 1119051810
Using Arrays.hashCode(): A and C have identical hash codes
a = 1250930
b = 1324
c = 1250930
Using Arrays.deepHashCode(): A and C have identical hash codes
a = 1250961
b = 1355
c = 1250961

以下是 Arrays.deepHashCode 的必要示例

import java.util.Arrays;

public class B {
    public static void main(String[] args) {
        Object[] d = {"abc", "def", new String[]{"ghi"}};
        Object[] e = {"abc", "def", new String[]{"ghi"}};

        System.out.println("NOTE: D and E have identical nested values");
        System.out.println("Using Object[].hashCode(): different");
        System.out.println("d = " + d.hashCode());
        System.out.println("f = " + e.hashCode());

        System.out.println("Using Arrays.hashCode(): still different");
        System.out.println("d = " + Arrays.hashCode(d));
        System.out.println("e = " + Arrays.hashCode(e));

        System.out.println("Using Arrays.deepHashCode(): identical");
        System.out.println("d = " + Arrays.deepHashCode(d));
        System.out.println("e = " + Arrays.deepHashCode(e));
    }
}

输出:

NOTE: D and E have identical nested values
Using Object[].hashCode(): different
d = 241990244
f = 1943487137
Using Arrays.hashCode(): still different
d = 1057745997
e = 709187068
Using Arrays.deepHashCode(): identical
d = 95807651
e = 95807651

答案 2 :(得分:1)

如果你想要包含相同字节的两个字节数组具有等效的哈希码,那么使用 deepHashCode 确实是正确的,你只需要对byte []数组进行一些额外的转换。

import java.utils.Arrays;

public class A {
  public static void main(String[] args) {
    byte[] a = {10,32,-43,80};
    byte[] b = {13,-40};
    byte[] c = {10,32,-43,80};
    // A and C will have different hash codes
    System.out.println(a.hashCode());
    System.out.println(b.hashCode());
    System.out.println(c.hashCode());
    // A and C will now have equivalent hash codes
    System.out.println(Arrays.deepHashCode(new Object[]{a}));
    System.out.println(Arrays.deepHashCode(new Object[]{b}));
    System.out.println(Arrays.deepHashCode(new Object[]{c}));
  }
}

这导致输出类似于......

// Hash Codes
a = 16130931
b = 26315233
c = 32716405
// Deep hash codes
a = 1250961
b = 1355
c = 1250961