I want to store key-value pairs <String, List> in a map and sort the entries based on the value (list of Data) of Key as per following logic:
- Sort the value (in the list) for each key (group) by the score in the Data objects and
- If the size of the map is greater than n (say n = 3) - then sort the keys(group) based on first item's (value) score and return 3. - equivalent to saying get Top 3(1 from each group) based on high score
I am looking get 1 result per group (A,B,C,D)
import java.util.ArrayList;
public class MyClass {
public static void main(String args[]) {
// output should be just Data(17.0, "five", "D"), Data(4.0, "two", "A"), Data(3.0, "three", "B")
ArrayList<Data> dataList = new ArrayList<Data>();
dataList.add(new Data(1.0, "one", "A"));
dataList.add(new Data(4.0, "two", "A"));
dataList.add(new Data(3.0, "three", "B"));
dataList.add(new Data(2.0, "four", "C"));
dataList.add(new Data(7.0, "five", "D"));
dataList.add(new Data(17.0, "five", "D"));
// output should be just Data(5.0, "six", "A"), Data(3.14, "two", "B"), Data(3.14, "three", "C")
ArrayList<Data> dataList2 = new ArrayList<Data>();
dataList2.add(new Data(3.0, "one", "A"));
dataList2.add(new Data(5.0, "six", "A"));
dataList2.add(new Data(3.14, "two", "B"));
dataList2.add(new Data(3.14, "three", "C"));
System.out.println("data 1= " + dataList.size());
System.out.println("data 2 = " + dataList2.size());
}
static class Data {
double score;
String name;
String group;
public Data(double score, String name, String group) {
score = this.score;
name = this.name;
group = this.group;
}
public String getName() {
return name;
}
public String getGroup() {
return group;
}
public double getScore() {
return score;
}
}
}
I know the procedural way to do this but is there a smarter/functional way to do it in Java 8?