• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object lists compare

 
auvrm papu
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 3 lists of objects,

for example: lets take numbers

A->[1, 2, 3, 4, 5]

B->[1, 3, 5]

C->[1, 2, 3, 4]

I want the D list to have only the common elements in all 3 of them that is,

D->[1,3]

also,

incase if any one of the list has no element -> compare the other two only
and
if any two are blank list, fetch the other entire full list as D


what is the best way to do this in java.

Thanks!
[ April 24, 2007: Message edited by: Ais Kaly ]
 
Stephen Paulsen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the java.util.Set retainAll() method
at http://java.sun.com/j2se/1.5.0/docs/api/java/util/Set.html
and test for empty sets before applying this to them.

Retains only the elements in this set that are contained in the specified collection (optional operation). In other words, removes from this set all of its elements that are not contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the intersection of the two sets.

Originally posted by Ais Kaly:
I have 3 lists of objects,

for example: lets take numbers

A->[1, 2, 3, 4, 5]

B->[1, 3, 5]

C->[1, 2, 3, 4]

I want the D list to have only the common elements in all 3 of them that is,

D->[1,3]

also,

incase if any one of the list has no element -> compare the other two only
and
if any two are blank list, fetch the other entire full list as D


what is the best way to do this in java.

Thanks!

[ April 24, 2007: Message edited by: Ais Kaly ]

[ April 24, 2007: Message edited by: Stephen Paulsen ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic