Forums Register Login

how to combine the objects in arraylist based on the object's field

+Pie Number of slices to send: Send
For example, arraylist1 of arraylist2:

[[195.03, 500, MYC], [195.03, 200, MYC], [195.01, 200, MYC], [194.98, 400, MYC], [194.95, 600, NYC]]

I want to combine the first 2 arraylists since they have the same field one(195.03) and same field 3(MYC), and add the second field together(500+200). After combination the output is:

[[195.03, 700, MYC], [195.01, 200, MYC], [194.98, 400, MYC], [194.95, 600, NYC]]

Thanks!!!

[ May 01, 2008: Message edited by: sheng zhong ]
[ May 01, 2008: Message edited by: sheng zhong ]
+Pie Number of slices to send: Send
Awkward.
I think you actually have a List of 4 objects, and you ought to get your elements into 4 objects. A List of Lists looks awkward to handle.

Then try sorting the List with Comparators for the 1st and last elements and iterate through the List with a for loop and combine pairs as appropriate.

It is likely there will be other ways to do it.
Anybody else got a better idea?
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1352 times.
Similar Threads
Help with ArrayList
A question for our Indian contingent..
Is it possible to set the response status code in Apache using JAVA
problem about vector
About an Object reference being passed to a method call
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:14:59.