简体   繁体   English

可以使用员工 class 作为 HashMap 中的键吗?

[英]Can one use an Employee class as a key in a HashMap?

Explain: Can one use an Employee class as a key in a HashMap?说明:可以使用 Employee class 作为 HashMap 中的键吗?

  • Knows about overriding hashCode了解覆盖 hashCode
  • Knows about overriding equals了解覆盖等于
  • Knows the behavior if hashCode returns a constant (including get and put time complexity)知道 hashCode 返回常量时的行为(包括 get 和 put 时间复杂度)
  • Knows the behavior if hashCode is not implemented but equals is知道 hashCode 未实现但 equals 是的行为
  • Knows the behavior if hashCode is implemented but equals is not如果实现了 hashCode 但未实现 equals 则知道行为
  • Knows that making the class immutable is a good idea when using it as a key.知道将 class 用作密钥时不可变是一个好主意。

Yes, if you set up the Employee class correctly.是的,如果您正确设置了Employee class。 That list of things you posted is what you 'prove' by being able to do this.您发布的内容列表就是您通过执行此操作“证明”的内容。

The javadoc of Object.equals and the javadoc of Object.hashCode list all sorts of caveats, such as: equality must be reflexive (that's just one of many). Object.equalsjavadoc 和Object.hashCode的 javadoc列出了各种注意事项,例如:相等必须是自反的(这只是其中之一)。

If you adhere to all of these caveats, then you can use a class as keys in a hashmap.如果您遵守所有这些注意事项,那么您可以使用 class 作为 hashmap 中的键。

It seems fairly clear, given that you've asked this question in this fashion, that,鉴于您以这种方式提出了这个问题,这似乎很清楚,

[A] you don't know about this stuff, and [B] it's part of a study requirement or job application. [A] 你不知道这些东西,[B] 这是学习要求或工作申请的一部分。

The conclusion is fairly simple: right now , you fail at this task - you don't know about any of this.结论很简单:现在,你在这个任务上失败了——你对这一切一无所知。 Fortunately, the bulleted list, along with the documentation of equals and hashCode as linked above, gives you the perfect opportunity to fix that.幸运的是,项目符号列表以及上面链接的 equals 和 hashCode 文档为您提供了解决此问题的绝佳机会。 Start experimenting, Write a class, and start implementing hashCode and equals.开始实验,写一个class,开始实现hashCode和equals。 and see what happens, Do the things the bullets describe (implements equals but not hashcode and see what happens when you create 2 seemingly identical Employee objects and note how you can then add both to a hashmap. breaking the rule that identical 'keys' override each other).看看会发生什么,做项目符号描述的事情(实现等于但不是哈希码,看看当你创建 2 个看似相同的 Employee 对象时会发生什么,并注意如何将两者都添加到 hashmap。违反相同“键”覆盖的规则彼此)。

One thing to keep in mind:要记住一件事:

In order to see the problems of eg implementing hashCode but not equals and vice versa, it's crucial you make separate objects.为了看到例如实现 hashCode 但不等于或反之亦然的问题,创建单独的对象至关重要。 You want something like:你想要这样的东西:

Employee a = new Employee("Pooja");
Employee b = new Employee("Pooja");
Map<Employee, String> test = new HashMap<>();
test.put(a, "hello");
test.put(b, "world");
System.out.println(test.size());

This should print '1'.这应该打印“1”。 If you don't properly do what the docs say, it will print 2. Experiment with what it takes to make that correctly print 1. Experiment with what happens when you intentionally mess up the implementation of eg hashCode and/or equals.如果您没有正确执行文档所说的操作,它将打印 2. 尝试正确打印所需的内容 1. 尝试当您故意弄乱例如 hashCode 和/或 equals 的实现时会发生什么。 Figure out what happens if the hashCode is the same but equals says they are not, etcetera.弄清楚如果 hashCode 相同但 equals 表示它们不一样会发生什么,等等。

Something like this:像这样的东西:

Employee a = new Employee("Pooja");
Map<Employee, String> test = new HashMap<>();
test.put(a, "hello");
test.put(a, "world"); // or
Employee b = a;
test.put(b, "goodbye");

isn't particularly useful to observe what happens when you 'mess' with equals and hashCode.观察当你“弄乱”equals和hashCode时会发生什么并不是特别有用。

Spend an hour or two and you'd ace this question / test / qualification.花一两个小时,你就会在这个问题/测试/资格认证中获得高分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM