向 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
Can I get an idea of how the hashcode takes the valueas per the element added to 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());
The hashcode values are
80
2530
78491
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它对于 List 接口的每个(真正的)实现都是相同的(如果它支持添加元素)。
.hashCode
方法的行为在List.hashCode()
如下:正如glowcoder 所示,
AbstractList
只包含此实现,因此并非List
接口的每个实现者都必须再次执行此操作。例如,您还可以编写
Arrays.asList("1", "2").hashCode()
并得到相同的 2530(只要您不更改hashCode ()
String
的实现)。It works the same for every (true) implementation of the List interface (if it supports adding elements). The behavior of the
.hashCode
method is defined inList.hashCode()
as follows:As glowcoder showed,
AbstractList
contains just this implementation, and thus not every implementor of theList
interface has to do this again.For example, you also could write
Arrays.asList("1", "2").hashCode()
and would get the same 2530 (as long as you don't change thehashCode()
implementation ofString
).因为 Vector 扩展了 AbstractList,所以它使用它作为它的 hashCode。这就是它的作用。
Because Vector extends AbstractList, it uses it for it's hashCode. Here's what it does.