Intersection of java.util.Map
31,033
Solution 1
How about:
Map map1 = ...;
Map map2 = ...;
Map result = new ...(map1);
result.keySet().retainAll(map2.keySet());
or:
Map map1 = ...;
Map map2 = ...;
Set result = new ...(map1.keySet());
result.retainAll(map2.keySet());
Solution 2
If you're using Guava, you can use Maps.difference
to get a MapDifference
object, from which you can extract the entriesInCommon()
and entriesDiffering()
as maps. (Disclosure: I contribute to Guava.)
Solution 3
Guava's Sets.intersection(Set, Set)
should do the job, with the keySet of each Map passed in as parameters.
Related videos on Youtube
Comments
-
mandy almost 2 years
Is there a method in
java.util.Map
or any util to perform an intersection on two maps? (To intersect two maps by the "keys")I am not able to find any. I can always implement my own intersection logic, but I was hoping there is already some operation in one of the
java.util.*
classes that would do this.-
dashrb over 11 yearsDo you want to see if the keys of one map are also keys in the other map? Do the values also matter, or just the keys?
-
mandy over 11 years@dashrb: just the keys, not the values.
-
Pikachu almost 8 years@mandy, please update your question with this information. :-)
-
-
mandy over 11 yearsI have 2 maps, map1 and map2. I want to perform an intersection between map1 and map2. I would have to create a new Set if i try to use this approach.
-
ruakh over 11 yearsHow would you convert that back into a
Map
? -
Woot4Moo over 11 yearsThat is an optional operation, will it work for all such keySets?
-
darrengorman over 11 yearsWell that depends on what you want to do with the values corresponding to intersecting keys surely?
-
Martin Ellis over 11 yearsThat doesn't give you the intersection, though. It just tells you whether one set of keys is a subset of the other. Finding the intersection means returning the common keys/values.
-
Martin Ellis over 11 yearsBy 'that' you mean Set#retainAll(). It'll work with java.util.* collections, and the concurrent collections. I'd expect it to work with all popular implementations of mutable maps, but obviously you'd need to verify for the particular implementation. @ruakh: Good edit, thanks.
-
darrengorman over 11 yearsSo now you have a
Map
with the values frommap1
for intersecting keys. What about such values frommap2
? -
Martin Ellis over 11 yearsSee the clarification in the comments on the question: the keys are of interest, the values "do not matter".
-
darrengorman over 11 yearsI saw that, so why bother creating a new
result
Map at all? One of the implementations from Apache Commons or Guava will get the intersecting keys if that's all @mandy is interested in. -
Martin Ellis over 11 yearsFair point, I've updated the answer to include an option for just obtaining a set. I suggested a JRE method because it seems likely to be more applicable. That is: not everyone can easily add dependencies to their application (for example, because binary size is a problem, bureaucracy, etc), nor necessarily wants to for something that they can easily do without.
-
Louis Wasserman over 11 yearsThe OP appears to only care about the keys, so it's fine.
-
Roland over 7 yearsThe OP seems to be only be concerned about the keys. The Guava library is not clearly documented in this respect, but it seems to be comparing also the values, no? Please document appropriately.
-
Louis Wasserman over 7 yearsRead the docs for
MapDifference
-- it tells you thatentriesDiffering()
, for example, "returns an unmodifiable map describing keys that appear in both maps, but with different values." -
Partha almost 5 yearsSet intersection = new HashMap(); ? Really?
-
dashrb almost 5 yearshahaha oops. That should say "Map". I'll edit. (Thanks for noticing, after 7 years. I can only assume my real code has similar latent but obvious bugs!)
-
Partha almost 5 yearsYour code helped me, and that's why I cared to correct it :) Thanks!
-
brightmatter about 3 yearsI found second answer very useful. Not needing externals like Apache Commons or Guava is a real bonus (to me). I find an answer that is two lines need not be in the JRE. Perhaps this could be placed in your own Utils function and hardened against nulls and Exceptions, and the Set could/should be typed. I see no reason to include any of that in the answer as this answers the question concisely. Well done.