1

I have lists of different classes, say A and B respectively. Definition of these classes is as follows-

class A {
 int field1;
 int field2;
}

class B {
 int field1;
 int field3;
}

class C {
 int field1;
 int field2;
 int field3;
}

I want to perform a join operation (similar to database join) on A and B, over field1 and populate the result in C.

I can iterate over the lists and do it. But, just wanted to check whether there are any library methods which does similar sort of thing.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
Teja
  • 341
  • 2
  • 7
  • 18
  • I have never used it myself so I am not sure enough to post that as an answer. I have heard that LINQ provides an API to query collections the same way you'd query a relational database. I know there are LINQ implementations (or LINQ-like libraries) for Java, maybe you should look at those. – ARRG Feb 22 '12 at 16:19

3 Answers3

0

If these are the only fields you're using, just iterate manually.

For larger data sets, group your data into Collections and apply a Cartesian Product algorithm.

Community
  • 1
  • 1
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
  • Actually, there are many fields in each class. Just to make it simple, I gave this example. – Teja Feb 22 '12 at 15:05
  • So A and B should probably contain a method that returns the value of the fields as a Collection. C should have a method that gets two collections as an argument, computes their Cartesian Product, and populates its own fields. – Adam Matan Feb 22 '12 at 15:47
0

You can add objects of any class to the list(if your list is not generic-specified). But when you get each object back from the list, you will not know the object is of which type.

sgowd
  • 2,242
  • 22
  • 29
-1

Maybe by using a method in library in apache collections:

import org.apache.commons.collections.ListUtils;    

List<?> newListC = ListUtils.intersection(list1, list2);

Hope it helps.

aretai
  • 1,619
  • 6
  • 19
  • 30