ArrayList, contains, objects
Hi,
I receive an arraylist with several objects like this,
publicclass SomeObject{
privatefinal String name;
privatefinalint quantity;
privatefinaldouble value;
public SomeObject(String name,int quantity,double value){
this.name = name;
this.quantity = quantity;
this.value = value;
}
//getters
publicint hashcode{
//code
}
//2 objects are equal if their names are equals
publicboolean equals(object obj){
boolean result =false;
if(objinstanceof SomeObject)
result = this.name.equals(((SomeObject)obj).getName());
return result;
}
}
but my arraylist contains equal objects (same name but different quantities and values). What I want to do is create an ArrayList with the distinct objects but summing the quantities and values.
Example
What I have
List list =new ArrayList();
SomeObject so =new SomeObject("SCO", 10, 10.1);
list.add(so);
SomeObject so =new SomeObject("SCO", 20, 10.2);
list.add(so);
SomeObject so =new SomeObject("SEF", 1, 1.1);
list.add(so);
//and so on...
and I want to create a new ArrayList containing
SomeObject("SCO", 30, 20.3);
SomeObject("SEF", 1, 1.1);
Can you give some ideias?
thanks in advance,
Manuel Leiria

