Java Streams: group a List into a Map of Maps

You can group your data in one go assuming there are only distinct Foo:

Map<String, Map<String, Foo>> map = list.stream()
        .collect(Collectors.groupingBy(f -> f.b.id, 
                 Collectors.toMap(f -> f.b.date, Function.identity())));

Saving some characters by using static imports:

Map<String, Map<String, Foo>> map = list.stream()
        .collect(groupingBy(f -> f.b.id, toMap(f -> f.b.date, identity())));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)