Reputation: 460
I have a List of HashMaps that are
1. Key/value key/Value
CODE=1 TOTAL=10
2. Key/value key/Value
CODE=1 TOTAL=10
3. Key/value key/Value
CODE=2 TOTAL=10
4. Key/value key/Value
CODE=2 TOTAL=10
5. Key/value key/Value
CODE=3 TOTAL=10
I want to get SUM total of the TOTAL columns using CODE as key in java for loop etc
i.e.
iterate over the list for each key, show sum (added together) total
console output should be
key with CODE 1 has total of 20 as sum
key with CODE 2 has total of 20 as sum
key with CODE 3 has total of 10 as sum
I have tried by using
int total = 0;
String previousCodeValue = "";
for(int i = 0; i < mapsList ; i++)
{
Map map = mapsList.get(i);
if(map.get("CODE") != previousCodeValue && !previousCodeValue.equals("") )
{
system.out.print(total);
total = 0;
}
previousCodeValue = map.get("CODE") ;
total = total + map.get("TOTAL")
}
The issue is the loops runs fine as long as CODE remains changing, but on the last code value, the logic will not work :( what to do ?
Regards,
Aiden
Upvotes: 0
Views: 1189
Reputation: 16209
So if I understand correctly you need the SUM of the TOTAL values for each unique KEY over all the maps. Supposedly you would want to do this in one iteration so you need to keep track of unique keys and totals as you go along. Below I keep track of the results in a map (yes, another map, it's just an example).
private static void main(String[] args) {
printResults(determineResults(createMaps()));
}
private static List<Map<String, Integer>> createMaps() {
List<Map<String, Integer>> maps = new ArrayList<>();
maps.add(createMap(1, 10));
maps.add(createMap(1, 10));
maps.add(createMap(2, 10));
maps.add(createMap(2, 10));
maps.add(createMap(3, 10));
return maps;
}
private static Map<String, Integer> createMap(int code, int total) {
Map<String, Integer> map = new HashMap<>();
map.put("CODE", code);
map.put("TOTAL", total);
return map;
}
private static Map<Integer, Integer> determineResults(List<Map<String, Integer>> maps) {
Map<Integer, Integer> results = new HashMap<>();
for(Map<String, Integer) map : maps) {
Integer key = map.get("CODE");
Integer sum = results.get(key);
if (sum == null) {
sum = 0;
}
Integer total = map.get("TOTAL");
sum += total;
results.put(key, sum);
}
return results;
}
private static void printResults(Map<Integer, Integer> results) {
for(Map.Entry<Integer, Integer> result : results) {
System.out.printf("key with CODE %s has total of %s as sum", result .getKey(), result.getValue());
}
}
Upvotes: 0
Reputation: 8387
Try to use Iterator
like this:
int totale = 0;
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry tot = (Map.Entry)it.next();
totale += tot.getValue();
}
Upvotes: 1
Reputation: 393841
It appears all you are missing is outputing the last total after the loop is done :
int total = 0;
String previousCodeValue = "";
for(int i = 0; i < mapsList ; i++)
{
Map map = mapsList.get(i);
if(map.get("CODE") != previousCodeValue && !previousCodeValue.equals("") )
{
System.out.print(total);
total = 0;
}
previousCodeValue = map.get("CODE") ;
total = total + map.get("TOTAL")
}
System.out.print(total);
Upvotes: 0