java 如何为对象列表正确定义哈希函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3054449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:07:29  来源:igfitidea点击:

How to properly define hash function for a list of objects?

javaalgorithmhash

提问by ansgri

I have a data structure containing a list of objects, like this:

我有一个包含对象列表的数据结构,如下所示:

class A {
  private List<Object> list;
}

How to properly define a hash function for the list, assuming each element of the list has a correct hashCode()?

如何正确定义列表的哈希函数,假设列表的每个元素都有一个正确的hashCode()

回答by Dirk

If the actual Listimplementation is fully conformant to the interface, the provided hashCodeimplementation should be sufficient:

如果实际List实现完全符合接口,则提供的hashCode实现应该足够了:

Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:

返回此列表的哈希码值。列表的哈希码定义为以下计算的结果:

hashCode = 1;
  Iterator i = list.iterator();
  while (i.hasNext()) {
      Object obj = i.next();
      hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  }

(List documentation)

列表文档

The Listinterface requires conforming implementations to provide equalsbased on the elements of the list. Thus, they had to specify the hashCodealgorithm explicitely

List接口需要equals基于列表元素提供的一致实现。因此,他们必须hashCode明确指定算法

回答by Péter T?r?k

Why do you want to define hashCodefor your list, when it already has it implemented(along with equals)?

为什么要hashCode为您的列表定义,当它已经实现(连同equals)?

(Provided it is java.util.Listof course - however if not, the link above shows you the exact implementation you can use for your own list type.)

java.util.List当然,前提是它是- 但如果不是,上面的链接会向您展示可用于您自己的列表类型的确切实现。)

回答by Pete Kirkham

The hash code of a list is defined by the List interfaceof List. This can be used as part of your object's hash code, though there are a couple of cases where you might not want to use it - if the elements of your list have an expensive hash code function, or if the list can hold a reference to the object, and you would then get a stack overflow if the list's algorithm was used. In that case, just use the length of the list or another hash value.

列表的哈希码由List 的 List 接口定义。这可以用作对象的哈希码的一部分,但在某些情况下您可能不想使用它 - 如果列表的元素具有昂贵的哈希码函数,或者列表可以保存对对象,然后如果使用列表的算法,您将获得堆栈溢出。在这种情况下,只需使用列表的长度或其他哈希值。

回答by Devon_C_Miller

In the Java library, Listimplementations (LinkedList, ArrayList) use the default hashCodeimplementation provided by AbstractList. which is defined as:

在 Java 库中,List实现 ( LinkedList, ArrayList) 使用由hashCode提供的默认实现AbstractList。定义为:

int hashCode = 1;
Iterator<E> i = iterator();
while (i.hasNext()) {
    E obj = i.next();
    hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}
return hashCode;

回答by Bill

Any specific reason why you just wouldn't do:

您不这样做的任何具体原因:

Arrays.hashCode(<cast list to array>);

Something like:

就像是:

Arrays.hashCode((String []) myList.toArray());