I have a person class
public class Person {
private int id;
private int amount;
public Person(int id, int amount){
this.id = id;
this.amount = amount;
}
//getters
}
And I have a mainClass like this
Map<String, Person> mapOfObjects = new HashMap<String, Person>();
mapOfObjects.put("map1", new Person(1, 1000));
mapOfObjects.put("map2", new Person(1, 1000));
mapOfObjects.put("map3", new Person(1, 1000));
mapOfObjects.put("map4", new Person(2, 1000));
mapOfObjects.put("map5", new Person(2, 1000));
mapOfObjects.put("map6", new Person(3, 1000));
Map<Integer, Integer> mapOfSum = new HashMap<Integer, Integer>();
int sum = 0;
List list = new LinkedList(mapOfObjects.keySet());
for (int i = 0; i < mapOfObjects.size(); i++) {
for (int j = 1; j < mapOfObjects.size() - i; j++) {
if (mapOfObjects.get(list.get(i)).getId() ==
mapOfObjects.get(list.get(j)).getId()) {
sum += (mapOfObjects.get(list.get(i)).getAmount() +
mapOfObjects.get(list.get(j)).getAmount());
}
mapOfSum.put(mapOfObjects.get(list.get(i)).getId(), sum);
}
}
System.out.println(mapOfSum);
It gives me output:
{1=8000, 2=8000, 3=0}
but i want something like this
id=1 amount =3000, id =2 amount = 2000, id =3 amount =1000
How can i remove the object from the map whose summation is already done once while running the first for-loop