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

[2720 byte] By [manuel.leiriaa] at [2007-11-27 11:32:46]
# 1

hi,

ArrayList list = new ArrayList();

list.add( new SomeObject("SCO", 10, 10.1) );

list.add( new SomeObject("SCO", 20, 10.2) );

list.add( new SomeObject("SCO", 20, 10.3) );

get the size of arraylistlist.size();

store in a temp var,

start a loop and get one by one list.get(i),

store into SomeObject like same,

add it x[0].a + x[1].a ... n

finally store the sum value and create new arraylist

and add that sum value,

its like a round trip,

u have to sort all datas

add with groups

drvijayy2k2a at 2007-7-29 16:47:21 > top of Java-index,Java Essentials,Java Programming...
# 2

Use a Map instead, and use the name as key. Always check if the map already contains an object with the key, and increase quantity and value if it does.

Kaj

kajbja at 2007-7-29 16:47:21 > top of Java-index,Java Essentials,Java Programming...
# 3

> hi,

>

>

> > ArrayList list = new ArrayList();

> list.add( new SomeObject("SCO", 10, 10.1) );

> list.add( new SomeObject("SCO", 20, 10.2) );

> list.add( new SomeObject("SCO", 20, 10.3) );

>

>

>

>

> get the size of arraylistlist.size();

> store in a temp var,

> start a loop and get one by one list.get(i),

> store into SomeObject like same,

> add it x[0].a + x[1].a ... n

> finally store the sum value and create new arraylist

> and add that sum value,

>

> its like a round trip,

> u have to sort all datas

> add with groups

Thanks for the reply.

That's what I did and, of course it works, but I was looking for something more fancy.

manuel.leiriaa at 2007-7-29 16:47:21 > top of Java-index,Java Essentials,Java Programming...
# 4

> Use a Map instead, and use the name as key. Always

> check if the map already contains an object with the

> key, and increase quantity and value if it does.

>

> Kaj

Sounds good. Let me give a try!

thanks,

manuel.leiriaa at 2007-7-29 16:47:21 > top of Java-index,Java Essentials,Java Programming...