I have a map as : Map<Integer, Map<LocalDate, CustomObject>>
I want to perform operations on each value of the inner map. sample example of my data in map :
<1, Map<2018-12-01, "A">
<2018-12-02, "A">
<2018-12-03, "A">
<2018-12-04, "A">
<2018-12-05, "A">>
<2, Map<2018-12-01, "B">
<2018-12-02, "B">
<2018-12-03, "B">
<2018-12-04, "B">
<2018-12-05, "B">>
<3, Map<2018-12-01, "C">
<2018-12-02, "C">
<2018-12-03, "C">
<2018-12-04, "C">
<2018-12-05, "C">>
I have tried adding the values of map as a list of map; Like :
List<Map<LocalDate, CustomObject>>
But I am unable to achieve the operation that I want to perform
What I need is to add all the elements key wise and generate a resultant Map. ie; I need some thing like INTO A RESULTANT MAP
2018-12-01 : A+B+C
2018-12-02 : A+B+C
2018-12-03 : A+B+C
You can use flatMap
to obtain a Stream
of the entries of all the inner Maps, and then collect them as you wish:
Map<Integer, Map<LocalDate, CustomObject>> input = ...
Map<LocalDate,List<CustomObject>> grouped =
input.values() // Collection<Map<LocalDate, CustomObject>>
.stream() // Stream<Map<LocalDate, CustomObject>>
.flatMap(m -> m.entrySet().stream()) // Stream<Map.Entry<LocalDate, CustomObject>>
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue,
Collectors.toList())));
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras