Java地图>>可能有内在类型吗?

发布于 2024-12-11 16:31:22 字数 86 浏览 0 评论 0原文

这怎么可能: HashMap 而 byte[] 的 hash() 是什么?

How is this possible:
HashMap<byte[], byte[]> and what is hash() of byte[]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2024-12-18 16:31:22

是的,这是可能的(有一个很大的警告,见下文),但是 byte[] 不是“内在类型”。首先,不存在这样的东西,您可能指的是“原始类型”。第二:byte[]不是原始类型,byte是。数组始终是引用类型。

数组没有特定的 hashCode 实现,因此它们只会使用 ObjecthashCode,表示hashCode将是 身份-hashCode,与实际内容无关。

换句话说:byte[] 是一个非常糟糕的Map 键,因为您只能检索具有完全相同的实例的值。

如果需要基于数组的内容hashCode(),可以使用Arrays.hashCode(),但这不会(直接)帮助您这地图。还有 Arrays.equals() 检查内容是否相等。

可以byte[]包装在一个实现hashCode()equals()的薄包装对象中(使用上面提到的方法):

import java.util.Arrays;

public final class ArrayWrapper {
  private final byte[] data;
  private final int hash;

  public ArrayWrapper(final byte[] data) {
    // strictly speaking we should make a defensive copy here,
    // but I *assume* (and should document) that the argument
    // passed in here should not be changed
    this.data = data;
    this.hash = Arrays.hashCode(data);
  }

  @Override
  public int hashCode() {
    return hash
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof ArrayWrapper)) {
      return false;
    }
    ArrayWrapper other = (ArrayWrapper) o;
    return this.hash == other.hash && Arrays.equals(this.data, other.data);
  }
  // don't add getData to prevent having to do a defensive copy of data
}

使用此类,您可以使用 Map

Yes, it is possible (with a big caveat, see below), but byte[] is not an "intrinsic type". First, there's no such thing, you probably mean a "primitive type". Second: byte[] is not a primitive type, byte is. An array is always a reference type.

Arrays don't have specific hashCode implementations, so they'll just use the hashCode of Object, which means that the hashCode will be the indentity-hashCode, which is independent from the actual content.

In other words: a byte[] is a very bad Map key, because you can only retrieve the value with the exact same instance.

If you need a content-based hashCode() based on an array, you can use Arrays.hashCode(), but that won't help you (directly) with the Map. There's also Arrays.equals() to check for content equality.

You could wrap your byte[] in a thin wrapper object that implements hashCode() and equals() (using the methods mentioned above):

import java.util.Arrays;

public final class ArrayWrapper {
  private final byte[] data;
  private final int hash;

  public ArrayWrapper(final byte[] data) {
    // strictly speaking we should make a defensive copy here,
    // but I *assume* (and should document) that the argument
    // passed in here should not be changed
    this.data = data;
    this.hash = Arrays.hashCode(data);
  }

  @Override
  public int hashCode() {
    return hash
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof ArrayWrapper)) {
      return false;
    }
    ArrayWrapper other = (ArrayWrapper) o;
    return this.hash == other.hash && Arrays.equals(this.data, other.data);
  }
  // don't add getData to prevent having to do a defensive copy of data
}

Using this class you can then use a Map<ArrayWrapper,byte[]>.

缱绻入梦 2024-12-18 16:31:22

对于数组,hashCode() 使用 Object 的默认实现 - 通常是某种形式的内部对象地址。因此,如果此 HashMap 中的键是不同的数组,则该键被认为是唯一的,而不是在数组内容相等的情况下。

byte[] a = { 2, 3 };
byte[] b = { 2, 3 };
System.out.println(a.equals(b)); // false
Map<byte[], String> map = new HashMap<byte[], String>();
map.put(a, "A");
map.put(b, "B");
System.out.println(map); // {[B@37d2068d=B, [B@7ecec0c5=A}

For arrays hashCode() uses the default implementation from Object - typically some form of internal object address. As a result, key in this HashMap is considered unique if it is a different array, not if array contents are equal.

byte[] a = { 2, 3 };
byte[] b = { 2, 3 };
System.out.println(a.equals(b)); // false
Map<byte[], String> map = new HashMap<byte[], String>();
map.put(a, "A");
map.put(b, "B");
System.out.println(map); // {[B@37d2068d=B, [B@7ecec0c5=A}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文