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.

Share:
31,033

Related videos on Youtube

mandy
Author by

mandy

Like to learn things and try new things out.

Updated on July 09, 2022

Comments

  • mandy
    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
      dashrb over 11 years
      Do 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
      mandy over 11 years
      @dashrb: just the keys, not the values.
    • Pikachu
      Pikachu almost 8 years
      @mandy, please update your question with this information. :-)
  • mandy
    mandy over 11 years
    I 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
    ruakh over 11 years
    How would you convert that back into a Map?
  • Woot4Moo
    Woot4Moo over 11 years
    That is an optional operation, will it work for all such keySets?
  • darrengorman
    darrengorman over 11 years
    Well that depends on what you want to do with the values corresponding to intersecting keys surely?
  • Martin Ellis
    Martin Ellis over 11 years
    That 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
    Martin Ellis over 11 years
    By '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
    darrengorman over 11 years
    So now you have a Map with the values from map1 for intersecting keys. What about such values from map2?
  • Martin Ellis
    Martin Ellis over 11 years
    See the clarification in the comments on the question: the keys are of interest, the values "do not matter".
  • darrengorman
    darrengorman over 11 years
    I 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
    Martin Ellis over 11 years
    Fair 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
    Louis Wasserman over 11 years
    The OP appears to only care about the keys, so it's fine.
  • Roland
    Roland over 7 years
    The 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
    Louis Wasserman over 7 years
    Read the docs for MapDifference -- it tells you that entriesDiffering(), for example, "returns an unmodifiable map describing keys that appear in both maps, but with different values."
  • Partha
    Partha almost 5 years
    Set intersection = new HashMap(); ? Really?
  • dashrb
    dashrb almost 5 years
    hahaha 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
    Partha almost 5 years
    Your code helped me, and that's why I cared to correct it :) Thanks!
  • brightmatter
    brightmatter about 3 years
    I 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.