• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ArrayList, contains, objects

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.

Example
What I have



and I want to create a new ArrayList containing


Can you give some ideias?

thanks in advance,
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you have all the getX methods in SomeObject

CASE 1:

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.

 
Yeah, but is it art? What do you think tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic