I got this as a coding interview question. I was able to get the desired results and pass all the test cases. However I did not clear the interview round. I think it was because I did it the imperative style - multiple for loops, multiple HashMaps, etc. I am trying to achieve the same results using streams. Here is the problem:
Given a array String[][] ex: {{"Scott","85"},{"Scott","67"},{"Tiger","81"},{"Tiger","21"},{"Marissa","92"}}, find the person with highest average score. Below is the code snippet of as far as I could get.
public static void main(String[] args) {
String[][] scores = {{"Tee","63"},{"Tee","75"},{"Tee","84"},{"Dee","59"},{"Dee","77"},{"Dee","66"},{"Dee","71"},{"Lee","81"},{"Lee","78"},{"Lee","58"},{"Bree","98"},{"Bree","78"}};
List<List<String>> slist = Arrays.stream(scores)
.map(Arrays::asList)
.collect(Collectors.toList());
slist.stream().collect(Collectors.groupingBy(l->l.get(0)));
}
I am able to convert the String[][] to List<List<String>>. I am looking to stream this new list, group it by the 0th element, then sum it and then get the average.