Collections join

151 views
Skip to first unread message

Andrey Belyaev

unread,
Aug 13, 2010, 10:32:45 AM8/13/10
to lambdaj
Hi Mario,

Since recently I've been facing the task to join two or more
collections(or maps) by some key. Currently I need to index one of
those by some field and iterate over another with key look up. It
would be great if we had something like this in lambdaj:

Collection<ClassA> collA;
Collection<ClassB> collB;

Collection<ClassC> result=join(collA,collB,
where(on(ClassA.class).getF1(),equals(on(ClassB.class).getF2())),
ClassC.class,
on(ClassA.class).getF2(),on(ClassA.class).getF3(),on(ClassB.class).getF4(),on(ClassB.class).getF5());

Here we are joining collections A and B by the fields F1 in ClassA and
F2 in ClassB and resulting class is ClassC constructed with ClassA
fields F2 and F3 and ClassB fields F4 and F5.

Syntax can be another, hope you got the idea.

How do you think would it be possible to implement something like
this?

Mario Fusco

unread,
Aug 13, 2010, 5:25:30 PM8/13/10
to lam...@googlegroups.com
Hi Andrey,

I guess you already know there is a lambdaj feature called project that allows to convert a list of objects in another list of objects of a different class as in the following example:

http://code.google.com/p/lambdaj/wiki/LambdajFeatures#Projecting_object%27s_property_values


As for the join of 2 different collections I understand your need, but it is not completely clear to me how you would like to join them. In particular you wrote:

"Here we are joining collections A and B by the fields F1 in ClassA and F2 in ClassB""

But what are your expectations if there are 3 objects in the first list having the same value on field F1 together with 2 object of the second list with that value on F2. Are you suggesting a cartesian product of those objects resulting in the creation of 6 objects?

Cheers,
Mario

Andrey Belyaev

unread,
Aug 15, 2010, 10:58:01 AM8/15/10
to lambdaj
Hi Mario,

As for project - after I realized that there is such a feature in
lambdaj the idea with joins came to my mind at once.

Initially I was thinking about 1-to-1 and 1-to-many relationships. As
for me many-to-many is not very often case and so I guess cartesian
product would be ok.
Actually I wish to have exactly the same behaviour of join like in
relational databases including inner and outer joins(in outer join we
can fill absent values with null or default values).

Thanks,
Andrey

On 14 авг, 01:25, Mario Fusco <mario.fu...@gmail.com> wrote:
> Hi Andrey,
>
> I guess you already know there is a lambdaj feature called project that
> allows to convert a list of objects in another list of objects of a
> different class as in the following example:
>
> http://code.google.com/p/lambdaj/wiki/LambdajFeatures#Projecting_obje...

Mario Fusco

unread,
Aug 28, 2010, 1:05:36 PM8/28/10
to lambdaj
Hi Andrey,

sorry for the delay in my reply. I've been quite busy both at work and
preparing my talk at JavaOne.

As for the join feature you are suggesting I believe it could be
useful, but I am also worried about some technical details. For
example in order to use it, it must be assured that ClassA doesn't
inherited from ClassB or viceversa, because otherwise it could be
impossible to figure out if a given argument should be taken from the
first or the second collection of beans. And unfortunately it is not
possible to enforce such a constraint in a java method declaration.

Anyway, I will start experimenting with it and I will keep you updated
about how it is progressing.
Thanks again for your precious help.

Bye,
Mario
Reply all
Reply to author
Forward
0 new messages