• Post Reply Bookmark Topic Watch Topic
  • New Topic

retainAll for Map<String, String>  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the entry sets and do a retailAll on those.
 
Paolo Pinkel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will not check if keys andvalues are the same. Only the keys.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paolo Pinkel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Argh! I confused keySet and entrySet. Now it works. Thanks everyone!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!