• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Capturing sets of Common Elements from a list

 
Greenhorn
Posts: 1
Eclipse IDE Windows Vista Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

Good day!

I've bit of a problem here - The scenario is as below:

I've a list that consists of objects from a particular class. each of the objects contains instance variables like concentration, volume etc (ones i'am concerned here is concentration and volume alone)

Once the objects are populated from the Application based on user inputs, I need to compare the concentration values from this list of objects and classify apart the objects that have same concentrations and ones that have different.

If the concentrations are same, i need to add the volumes and display them, else if the concentrations differ , then i 've to display the volumes seperately.

How do i do this?. Would appreciate some help here.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sunil Baby wrote:How do i do this?. Would appreciate some help here.


My suggestion would be to create a Comparator that orders your objects by their concentration. That way you can then use Collections.sort() to sort your List by concentration. After that, the selection/grouping process is up to you; but it should be pretty straightforward.

Winston
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alternatively, you could use the same Comparator to add objects to a tree set. Note they will be sorted on insertion, but the original List will be unchanged.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Alternatively, you could use the same Comparator to add objects to a tree set. Note they will be sorted on insertion, but the original List will be unchanged.


A definite possibility, however OP should be aware that TreeSet will eliminate any objects with duplicate concentrations.

Winston
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may have misunderstood the original question, but thought he didn’t want duplicates. Obviously, if you do want duplicates, a tree set is useless.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic