0

I have multiple maps inside another Map.

Map<String,Double> map1 = new HashMap();
map1.put("value1",40.0);
map1.put("value2",35.0);
map1.put("value3",50.0);
Map<String,Double> map2 = new HashMap();
map2.put("value1",40.0);
map2.put("value4",25.0);
map2.put("value3",50.0);
Map<String,Double> map3 = new HashMap();
map3.put("value1",40.0);
map3.put("value2",35.0);
map3.put("value5",60.0);
Map<String,Map<String,Double>> parentMap = new HashMap();
parentMap.put("key1",map1);
parentMap.put("key2",map2);
parentMap.put("key3",map3);

I am able to do it using below logic but not able to do it using java 8 streams.

for (Map.Entry<String, Map<String, Double>> value : parentMap.entrySet()) {
//doing all the calculation
}

I am trying to get the below values:

  1. sum of all the values inside all map
  2. single map with the sum of similar key value
  3. top 3 keys with the sum value

Is it poosible to do it using java streams. I will update the solution if I find any.

One map can be summed by using below code.

map.getValue().entrySet().stream().mapToDouble(Entry::getValue).sum();
shershah
  • 57
  • 6

0 Answers0