• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capturing sets of Common Elements from a list

 
Sunil Baby
Greenhorn
Posts: 1
Eclipse IDE Tomcat Server Windows Vista
  • Mark post as helpful
  • send pies
  • 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.

 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Campbell Ritchie
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50189
79
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic