4

Suppose that I have a map of a set of strings to an integer value:
Map<HashSet<String>, Integer> map = new HashMap<>().

For example, the map is (we assume no duplicate strings):

{x,y}   ->  2
{z}     ->  3
{u,v,w} ->  4

How can I get another_map of type Map<String, Integer> as follows, using Java 8 Stream APIs:

x -> 2
y -> 2
z -> 3
u -> 4
v -> 4
w -> 4

It looks like a flatMap operation, but how could I associate the Integer values with each String key appropriately?

hengxin
  • 1,867
  • 2
  • 21
  • 42

1 Answers1

7

You can do this creating intermediate Map.Entry objects like this:

Map<String, Integer> result = map.entrySet().stream()
   .<Entry<String, Integer>>flatMap(entry -> 
       entry.getKey()
            .stream()
            .map(s -> new AbstractMap.SimpleImmutableEntry<>(s, entry.getValue())))
   .collect(Collectors.toMap(Entry::getKey, Entry::getValue));

Alternatively you may use any other pair/tuple type you have in your project.

Note that my free StreamEx library supports handling such cases in more clean way (internally it's the same as above):

Map<String, Integer> result = EntryStream.of(map).flatMapKeys(Set::stream).toMap();

The EntryStream class extends Stream<Map.Entry> and provides additional helpful methods like flatMapKeys or toMap.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334