简体   繁体   中英

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?

  • Knows about overriding hashCode
  • Knows about overriding equals
  • Knows the behavior if hashCode returns a constant (including get and put time complexity)
  • Knows the behavior if hashCode is not implemented but equals is
  • Knows the behavior if hashCode is implemented but equals is not
  • Knows that making the class immutable is a good idea when using it as a key.

Yes, if you set up the Employee class correctly. 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).

If you adhere to all of these caveats, then you can use a class as keys in a 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.

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. Start experimenting, Write a class, and start implementing hashCode and 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).

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. 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'. 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. Figure out what happens if the hashCode is the same but equals says they are not, etcetera.

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.

Spend an hour or two and you'd ace this question / test / qualification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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