| Author |
Capturing sets of Common Elements from a list
|
Sunil Baby
Greenhorn
Joined: Oct 15, 2011
Posts: 1
|
|
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.
|
Regards
Sunil
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4744
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
And welcome to the Ranch
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
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
Joined: Mar 17, 2011
Posts: 4744
|
|
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
Sheriff
Joined: Oct 13, 2005
Posts: 32651
|
|
|
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.
|
 |
 |
|
|
subject: Capturing sets of Common Elements from a list
|
|
|