为 HashMap 的对象列表生成 HashCode
我发现当我们将一个对象作为键插入到 Map 中时,它的哈希代码就会生成。但如果我的键是对象列表,在这种情况下,它是列表中所有对象的哈希码的总和吗?
User user1 = new User(13, "Ron", "[email protected]");
User user2 = new User(15, "Kamden", "[email protected]");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
Map<List<User>, User> userMap = new HashMap<>();
userMap.put(userList, user1);
我该如何理解这一点?
I got that when we insert an object into a Map as Key, its hash code gets generated. But if my key is list of objects, in that case, is it the sum of all the Hash Code of objects in the list ?
User user1 = new User(13, "Ron", "[email protected]");
User user2 = new User(15, "Kamden", "[email protected]");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
Map<List<User>, User> userMap = new HashMap<>();
userMap.put(userList, user1);
How can I understand this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上是在 JavaDoc 中指定的。
ArrayList javadocs 告诉您查看 AbstractList 中的实现,AbstractList.hashCode() 表示该实现与 List.hashCode 中给出的定义相同
That's actually specified in the JavaDoc.
The ArrayList javadocs tells you to look at the implementation in AbstractList, and AbstractList.hashCode() says the implementation is the same as in List.hashCode which gives this definition