This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes retainAll for Map<String, String> Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "retainAll for Map<String, String>" Watch "retainAll for Map<String, String>" New topic
Author

retainAll for Map<String, String>

Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
I have some TreeMaps and I need to do a retainAll. In the end, I want a TreeMap made out of the key-value-pairs that were the same for every TreeMap.

Any idea? It's not just that the keys need to be equal... it needs to be the key and the value. That's why I don't know how to handle the .keySet and .values.

Thanks!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Didn't test it out but: do a retainAll on the keys then a retainAll on the values and check whether the size of the set has changed.
Flaw in my logic.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Get the entry sets and do a retailAll on those.
Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
This will not check if keys andvalues are the same. Only the keys.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

The entry set will definitely check for both. It will use Map.Entry equality which says that two entries are equal only if both the keys and values are equal. So if a key has one value in the first map and a different value in the second map, their entries for that key will be different and retainAll will not retain that entry.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
This is how I try it:



tempMaps is a list of Map<String, String>. In the end, I want to have a map with the retainAll values of every Map.

Can you see why it won't work?

Your help is much appreciated!
Paolo Pinkel
Ranch Hand

Joined: May 04, 2011
Posts: 35
Argh! I confused keySet and entrySet. Now it works. Thanks everyone!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: retainAll for Map<String, String>
 
Similar Threads
Performing Math on Integer Class
Creating Stings Dynamically
Getting the Hashtable values by partial key
elegant way to reverse key/value in Map
Why & how to build map of maps?