将哈希码解释为Vector

我是否可以了解哈希码如何根据添加到向量的元素获取值?
  Vector v = new Vector();

  //Add elements to Vector
  v.add("1");
  System.out.println(v.hashCode());
  v.add("2");
  System.out.println(v.hashCode());
  v.add("=");
  System.out.println(v.hashCode());
哈希码值是
 80
 2530
 78491
    
已邀请:
它对List接口的每个(真正的)实现都是一样的(如果它支持添加元素)。
.hashCode
方法的行为在
List.hashCode()
中定义如下:   返回此列表的哈希码值。列表的哈希码被定义为以下计算的结果:
 int hashCode = 1;
 Iterator<E> i = list.iterator();
 while (i.hasNext()) {
     E obj = i.next();
     hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
 }
     这确保
list1.equals(list2)
意味着任何两个列表的
list1.hashCode()==list2.hashCode()
,   
list1
list2
,按照
Object.hashCode()
的总合同要求。 正如辉光编码器所示,
AbstractList
仅包含此实现,因此并非
List
接口的每个实现者都必须再次执行此操作。 例如,你也可以写
Arrays.asList("1", "2").hashCode()
并得到相同的2530(只要你不改变
hashCode()
hashCode()
实现)。     
因为Vector扩展了AbstractList,所以它将它用于hashCode。这是它的作用。
public int hashCode() {
int hashCode = 1;
Iterator<E> i = iterator();
while (i.hasNext()) {
    E obj = i.next();
    hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}
return hashCode;
}
    

要回复问题请先登录注册