aspose file tools*
The moose likes Java in General and the fly likes Capturing sets of Common Elements from a list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Capturing sets of Common Elements from a list " Watch "Capturing sets of Common Elements from a list " New topic
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: 8008
    
  22

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?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
And welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39380
    
  28
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: 8008
    
  22

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: 39380
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Capturing sets of Common Elements from a list