I have a Map of Lists with key = GROUP and value = List numbers stored as Strings. One element is below
GROUP1/[3.0, 4.0, 2.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, -2.0, -2.0, -2.0, 2.0]
What I have to do is to cancel off (remove) all the negative numbers against the positives but reduce the +ve numbers by that value.
In the above example, 3 will cancel off against -2 (first -ve) leaving +1 as the interim total
Then +1 (total from prev) will cancel off -2 (next -ve) leaving -1 as the interim total
-1 has to square against 4 (next +ve in the list) leaving 3 as the interim total
Then, 3 squares off against -2 (next -ve) leaving a total of 1.
Thus all -ve are eliminated from the List, so are 3.0, 4.0 but the first element is now 1.0 (1.0 being the last interim total)
Final sequence being
[1.0, 2.0, 3.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0]
The code I have so far just totals up the List as below.
Map<String, List<String>> m = new HashMap<String, List<String>>();
...
for (Entry<String, List<String>> entry : m.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
double calc = 0;
for (String element:entry.getValue()){
//System.out.println(element);
calc = calc + new Double(element).doubleValue();
}
//if (calc > 0.0)
System.out.println("Total for Item: " + entry.getKey() + " is "+calc);
}
Total for Item: GROUP1 is 19.0
I know we should not remove elements from the List as we iterate, so question is
A) Ideal Logic for removing the numbers in the sequence above.
B) Should I build a new List and add elements into it as I iterate ?
C) Should I change the collection I'm storing from Map of Lists to some class structure ?