I have use case like this where I need to aggregate values in a list by multiple group by but then calculate percentage of distribution of each of those values and create a new list.
An example of list of items:
week1 source1 destination1 100
week1 source1 destination2 200
week1 source2 destination1 200
week1 source2 destination2 100
week2 source1 destination1 200
week2 source1 destination2 200
From this I want to group by week and source and calculate the total quantity and then distribute percentage based on quantity.
As an example the total quantity for week 1 from source 1 is 300, which is going to destination 1(100) and destination 2(200). Now the percentage of distribution is for week 1 from source 1 to destination1 is 33.33% and for week1 from source 1 to destination 2 to 66.66%
For example the output would be:
week1 source1 destination1 33.33%
week1 source1 destination2 66.66%
week1 source2 destination1 66.66%
week1 source2 destination2 33.33%
week2 source1 destination1 50%
week2 source1 destination2 50%
How can I achieve this result using Java 8 streams.
Say I have list of these objects as List into "records" object:
public class Record {
private String sourceNode;
private String destinationNode;
private String weekIndex;
private String quantity;
}
Map<String, Map<String, List<Record>>> RecordsGroupByWeekAndSource = records.stream()
.collect(Collectors.groupingBy(Record::getWeekIndex, Collectors.groupingBy(Record::getSourceNode)));
This would give me the items group by week and source. But I will have to iterate this map again to calculate the total quantity in each list that resides inside map of map object. But Is there a way I can do this percenatage calcualtion within the groupingBy collection itslef?