Skip to content

Instantly share code, notes, and snippets.

@medusar
Created July 3, 2018 07:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medusar/ef0a54d016540bf604f94541eb490b9f to your computer and use it in GitHub Desktop.
Save medusar/ef0a54d016540bf604f94541eb490b9f to your computer and use it in GitHub Desktop.
how to flatten a nested map in java8
public void testFlattenMap() {
Map<String, Map<String, Long>> counters = new HashMap<>();
Map<String, Long> nested = new HashMap<>();
nested.put("inner1", 1L);
nested.put("inner2", 2L);
nested.put("inner3", 3L);
counters.put("out1", nested);
counters.put("out2", nested);
counters.put("out3", nested);
//change the counters map to Map<String,Long>
// the keys are like: out1.inner1,out1.inner2 ...
Stream<SimpleEntry<String, Long>> entryStream = counters.entrySet().stream()
.flatMap(e -> e.getValue().entrySet().stream().flatMap(
v -> Stream.of(new SimpleEntry<>(e.getKey() + "." + v.getKey(), v.getValue()))));
Map<String, Long> collect = entryStream
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
collect.forEach((k, v) -> {
System.out.println("k:" + k + ",v:" + v);
});
}
@medusar
Copy link
Author

medusar commented Jul 3, 2018

suppose you have a nested map like this:

Map<String,Map<String,Long>> map = new HashMap<>();

and data in the map are:

"outerKey1" --> Map
                         "innerKey1" -> 100L
                         "innerKey2" -> 100L
"outerKey2" --> Map
                         "innerKey1" -> 100L
                         "innerKey2" -> 100L

and you want to change the nested map to a map whose keys are composed of the outer keys and inner map keys, for example:

"outKey1.innerKey1" --> 100L
"outKey1.innerKey2" --> 100L
"outKey2.innerKey1" --> 100L
"outKey2.innerKey2" --> 100L

then the gist above is your solution !

@medusar
Copy link
Author

medusar commented Jul 3, 2018

notes: read the java docs about Stream.map and Stream.flatMap carefully, the difference is here:

Stream.map 

Returns a stream consisting of the results of applying the given function to the elements of this stream.
Stream.flatMap
Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

Steam.map does not replace the streams, it only do something on the elements of the stream.
Strem.flatMap replaces the elements of the stream with another stream, the new stream is produced by the function you provided

In a nutshell, Stream.map does not replace elements with streams, it works on the elements of the stream, Stream.flatMap replace elements with steams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment