I have this simple Bean class:
public class Book {
public Book(Map attribute) {
super();
this.attribute = attribute;
}
//
You could do it like this:
Map<String, List<String>> library =
books.stream()
.flatMap(b -> b.getAttribute().entrySet().stream())
.collect(groupingBy(Map.Entry::getKey,
mapping(Map.Entry::getValue, toList())));
From the Stream<Book>
, you flat map it with the stream of each map it contains so that you have a Stream<Entry<String, String>>
. From there you group the elements by the entries' key and map each entry to its value that you collect into a List for the values.