2

Collection Interface

It appears to me that this example code needs correction:

Map<Employee, Employee> m = new HashMap<Employee, Employee>(managers);
m.values().removeAll(managers.keySet());

Set<Employee> slackers = m.keySet(); // this line should be changed to

Set<Employee> slackers = m.values(); 

Can someone confirm?

5
  • Why the down vote? This is a valid question. If you think the correction is not needed, you can point that out to me. Commented Jun 28, 2015 at 0:03
  • Can you please explain why do you think so?? Please note I didn't down vote.. Commented Jun 28, 2015 at 0:10
  • @hagrawal Please read the text of the example and then the example code, you will likely see the mistake. Commented Jun 28, 2015 at 0:25
  • 2
    Probably that's the reason somebody down voted. Why to go through the all code snippets and understand your concern when you could have explained in few lines like ".... since .... is expected so I think values() is more appropriate than keySet()" .. I personally believe being self-explanatory in questions is a good thing. Commented Jun 28, 2015 at 0:32
  • 3
    @abc - Actually, I did, and actually I didn't see a mistake. Why do you think that line is mistaken?? Commented Jun 28, 2015 at 0:45

1 Answer 1

5

You can only know if that code needs correction if you know what it is supposed to be doing. Here is the context:

Once you've done this, you may have a bunch of employees whose managers no longer work for the company (if any of Simon's direct-reports were themselves managers). The following code will tell you which employees have managers who no longer works for the company.

    Map<Employee, Employee> m = new HashMap<Employee, Employee>(managers);
    m.values().removeAll(managers.keySet());
    Set<Employee> slackers = m.keySet();

So we start out with a map that maps every current employee to a manager. Then we remove all entries where the manager is a current employee. This leaves us with a map containing only entries for employees whose manager is not an employee. Finally, we get the keyset ... which gives us those employees as a set.

That seems correct to me1.

You suggest that the last line should be:

    Set<Employee> slackers = m.values();

That would give you all managers who have left. But that isn't the answer the problem is asking for. (And besides, m.values() will return a Collection not a Set.)


TL;DR - no mistake in the tutorial.


1 - Apart from the incorrect assumption that an employee without a manager will be a slacker :-).

Sign up to request clarification or add additional context in comments.

3 Comments

got it. I was focussed on finding managers who are no longer employees at the company. But the tutorial correctly finds employees whose managers are no longer employees at the company.
I don't see any modifications to the Map m here. m.values() returns a Collection containing all managers, then the code modifies that collection by removing some entries from it ( .removeAll(managers.keySet()) ). After all, m is not modified, which means it's still the same Map as the original managers Map. Can you explain?
@HieuM.Nguyen - Removing elements from the values() collection removes entries from the underlying map. The javadoc says so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.