TagGroupUtils { @NonNull public static Map<String, Set<String>> intersect(@NonNull Map<String, Set<String>> lh, @NonNull Map<String, Set<String>> rh) { Map<String, Set<String>> result = new HashMap<>(); for (Map.Entry<String, Set<String>> entry : lh.entrySet()) { String tagGroup = entry.getKey(); Set<String> rhTags = rh.get(tagGroup); if (rhTags != null) { Set<String> tags = new HashSet<>(rhTags); tags.retainAll(entry.getValue()); result.put(tagGroup, tags); } } return result; }  @NonNull static Map<String, Set<String>> union(@NonNull Map<String, Set<String>> lh, @NonNull Map<String, Set<String>> rh); static void addAll(@NonNull Map<String, Set<String>> lh, @NonNull Map<String, Set<String>> rh); static boolean containsAll(@NonNull Map<String, Set<String>> lh, @NonNull Map<String, Set<String>> rh); @NonNull static Map<String, Set<String>> intersect(@NonNull Map<String, Set<String>> lh, @NonNull Map<String, Set<String>> rh); @NonNull static Map<String, Set<String>> parseTags(@Nullable JsonValue value);  }
@Test public void testIntersect() { Map<String, Set<String>> tagGroupsOne = new HashMap<>(); tagGroupsOne.put("shared-group", tagSet("cool", "story")); tagGroupsOne.put("unique-group", tagSet("cool", "story")); Map<String, Set<String>> tagGroupsTwo = new HashMap<>(); tagGroupsTwo.put("shared-group", tagSet("cool")); Map<String, Set<String>> expected = new HashMap<>(); expected.put("shared-group", tagSet("cool")); assertEquals(expected, TagGroupUtils.intersect(tagGroupsOne, tagGroupsTwo)); assertEquals(expected, TagGroupUtils.intersect(tagGroupsTwo, tagGroupsOne)); }