I have key/value like this
("A", 2.2);
("A", 1.1);
("B", 4.0);
("B", 2.0);
("C", 2.5);
("A", 2.0);
("A", 2.2);
("A", 1.0);
I expect to have result is
A=3.3
B=6.0
C=2.5
A=5.2
I tried with code
static LinkedHashMap<String, Double> map = new LinkedHashMap<String, Double>();
public static void putAndIncrement(String key, double value) {
Double prev = map.get(key);
Double newValue = value;
if (prev != null) {
newValue += prev;
}
double roundOff = Math.round(newValue * 10.0) / 10.0;
map.put(key,roundOff);
}
However result is
A=8.5
B=6.0
C=2.5
Hashmap, Map, LinkedHashmap is not right way to get my expecting result. Can you consult me any other method for this situation ? Please help me how to get that or any suggestion is really helpful for me. Thank you