Meaningless Drivel is fun!
The moose likes Java in General and the fly likes ArrayList, contains, objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList, contains, objects" Watch "ArrayList, contains, objects" New topic

ArrayList, contains, objects

Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171

I receive an arraylist with several objects like this,

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.

What I have

and I want to create a new ArrayList containing

Can you give some ideias?

thanks in advance,

Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Vijayender Bandaru

Joined: Nov 21, 2006
Posts: 21
Assuming you have all the getX methods in SomeObject


If you have control over SomeObject and if you can modify it,then the solution is very simple.

1.remove final modifier for all instance varialbles.
2.In the equals method,whenever the object names are equal
add up the current obj values with new object

CASE 2: If you can't modify the SOmeObject class,here is the solution.

I agree. Here's the link:
subject: ArrayList, contains, objects
It's not a secret anymore!