HasMap1 has keys 100 , 200
HashMap2 has keys 200,300,400,100
I need to remove the elements with keys 300 and 400 which are also in HashMap1 from HashMap2. How to achieve that? I thought of different ways but the problem is even if I separate the duplicates keys how to make sure I remove only those from HashMap2.
Draw a diagram on a piece of paper and the logic will become obvious.
You need to be specific. By “also in HashMap1” do you mean they have the same “V”s?
Campbell Ritchie wrote:Draw a diagram on a piece of paper and the logic will become obvious.
You need to be specific. By “also in HashMap1” do you mean they have the same “V”s?
Alternatively instead of iterating, you can use the retainAll() method in Set interface. First get the key set of the second map and call retainAll() on it with the first map's keys. Check methods - keySet() and retainAll()