I have two maps with key as an integer and value as a double.
I want to create a third map sorted on key and value would be a list of doubles from both the maps.
map1:
1, 90.00
5, 75.45
8, 76.50
map2:
4, 12.00
5, 322.09
8, 11.09
9, 21.00
final map:
1, (90.00,0.00)
5, (75.45,322.09)
8, (76.50,11.09)
9, (0.00, 21.00)
As clear from above, if a key in one of the map is missing in the other map, the value in the final map for the other map should be defaulted to 0.00
Map firstMap = new HashMap<Integer, Double>();
Map secondMap = new HashMap<Integer, Double>();
Map finalMap = new HashMap<Integer, List<Double>>();
firstMap.put(1, 90.00);
firstMap.put(5, 75.45);
firstMap.put(8, 76.50);
secondMap.put(4, 12.00);
secondMap.put(5, 322.09);
secondMap.put(8, 11.09);
secondMap.put(9, 21.00);
I can use putAll method to put all keys into the third map. But how to put the values as I want ?
Thanks for reading!
Simply do
Using Guava it’s as simple as