Would you please help me to find the maximum value for each key from list of map: the list of map is like below:
[{47=96, 82=189, 100=231, 125=279, 158=322, 240=375},
{47=125, 82=239, 100=285, 125=334, 158=378, 240=429},
{47=119, 82=170, 100=182, 125=188, 158=188, 240=170}]
How to find max of value for each key? For example, the max value for the key 47 is 125, I need to find the max value of each key.
List<TreeMap<Integer, Integer>> listOfFreq = new ArrayList<>();
for (int thisFreq : frequencyList) {
int max = listOfFreq.stream()
.mapToInt(map -> map.getOrDefault(thisFreq, Integer.MIN_VALUE))
.max().orElse(0);
You can flatten the entrySet of map using flatMap
and collect as new map using Collectors.toMap
with max value as the value of the key.
Map<Integer, Integer> res =
listOfFreq.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), Integer::max));