I have a list of classes A as
class A { private Integer keyA; private Integer keyB; private String text; }
I want to pass aList nested Map displayed by keyA and keyB
So, I am creating below code.
Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap.put(e.getKey(), e.getValue().stream().collect(Collectors.groupingBy(A::getKeyB)));}); return nestedMap;}));
But I do not like this code.
I think if I use flatMap , I can better code this.
But I do not know how to use flatMap for this behavior.
source share