I have a list of maps.
List
The values in the list are, for example
<1, String1>
&l
You can do it the following with Java 8:
private void init() {
List
This will print:
Integer: 1 / List: [String1, String3]
Integer: 2 / List: [String2, String4]
Explanation (heavily warranted), I am afraid I cannot explain every single detail, you need to understand the basics of the Stream and Collectors API introduced in Java 8 first:
Stream from the mapList.flatMap operator, which roughly maps a stream into an already existing stream.Map to Stream> and add them to the existing stream, thus now it is also of type Stream> .Stream> into a Map> .Collectors.groupingBy, which produces a Map> based on a grouping function, a Function that maps the Map.Entry to an Integer in this case.Map.Entry::getKey, it operates on a Map.Entry and returns an Integer.Map>> if I had not done any extra processing.Collectors.groupingBy, which has to provide a collector.Map.Entry entries to their String values via the reference Map.Entry::getValue.Collectors.toList() here, as I want to add them to a list.Map> .