Issue
There is List<Map<String,String>>, as below:
[
{"A": "ABC","B": "ABCD","C": "0.945"},
{"A": "ABC","B": "AB","C": "0.871"},
{"A": "XYZ","B": "XYZA","C": "0.765"},
{"A": "ABC","B": "ABD","C": "0.945"},
{"A": "XYZ","B": "XYZB","C": "0.563"}
]
Question
Filter above List to get a Map where key as 'A' and highest value of 'C' for that 'A'.
{"ABC": "0.945", "XYZ": "0.765"}
Initial Code
Map<String, String> m0 = new HashMap<>();
m0.put("A", "ABC");
m0.put("B", "ABCD");
m0.put("C", "0.945");
Map<String, String> m1 = new HashMap<>();
m1.put("A", "ABC");
m1.put("B", "AB");
m1.put("C", "0.871");
Map<String, String> m2 = new HashMap<>();
m2.put("A", "XYZ");
m2.put("B", "XYZA");
m2.put("C", "0.765");
Map<String, String> m3 = new HashMap<>();
m3.put("A", "ABC");
m3.put("B", "ABD");
m3.put("C", "0.945");
Map<String, String> m4 = new HashMap<>();
m4.put("A", "XYZ");
m4.put("B", "XYZB");
m4.put("C", "0.563");
List<Map<String,String>> res = Arrays.asList(m0,m1,m2,m3,m4);
I am able to get:
[{A=ABC, B=ABCD, C=0.945}, {A=XYZ, B=XYZA, C=0.765}]
Using code below:
List<Map<String, String>> result = res.stream().collect(Collectors.groupingBy(map->map.get("A")))
.entrySet().stream().map(m -> m.getValue().stream()
.max(Comparator.comparingDouble(s -> Double.parseDouble(s.get("C")))).get())
.collect(Collectors.toList());
Solution
It's easier when you start by discarding unneeded entries and retaining only values of A
and C
as key/value pairs to work with.
Map<String, Double> result = res.stream()
.map(m -> Map.entry(m.get("A"),
Double.parseDouble(m.get("C"))))
.collect(Collectors.groupingBy(Entry::getKey,
Collectors.mapping(Entry::getValue,
Collectors.maxBy(Double::compare))))
.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> e.getValue().get()));
That produces {ABC=0.945, XYZ=0.765}
as result
, as required; but uses two streams to get rid of the Optional<Double>
type for values.
Answered By - ernest_k