File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Object lists compare Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object lists compare" Watch "Object lists compare" New topic
Author

Object lists compare

auvrm papu
Ranch Hand

Joined: Sep 01, 2005
Posts: 105
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

Joined: Sep 11, 2006
Posts: 9
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object lists compare