Issue
I cant figure how to do the following in java
In groovy, if I want to iterate over a map and inside that map the value is a list of lists. then get a specific index from a list of lists i.e. the following code will work
def total = value.collect { it.get(0) }*.toInteger().sum()
I read all lists with a specific index using the spread operator and convert all retrieved data to an integer and get the sum.
How do to this in Java?
Solution
You can use Java 8 stream API to iterate over outer list and fetch specific index form inner list. And then use mapToInt and sum function to retrieve required output.
Map<Integer, List<List<Integer>>> map = new HashMap<>();
int key = 8;
int index = 0;
int sum = map.get(key).stream().mapToInt(internalList -> internalList.get(index)).sum();
For you it would look something like this.
int total = value.stream().mapToInt(internalList-> internalList.get(index)).sum();
Answered By - P Mittal
Answer Checked By - David Marino (JavaFixing Volunteer)