eha
eha

Reputation: 87

Simulating a full outer join in Java

Let's say I have two arrays in Java and I wanted to preform a full outer join on them (returning a third array).

How would I go about doing this?

Upvotes: -1

Views: 3582

Answers (1)

Daniel Kaplan
Daniel Kaplan

Reputation: 67474

I believe that CollectionUtils from apache commons will have everything you need and more.

Check out these methods and their description:

union

public static java.util.Collection union(java.util.Collection a, java.util.Collection b)

Returns a Collection containing the union of the given Collections. The cardinality of each element in the returned Collection will be equal to the maximum of the cardinality of that element in the two given Collections.

Parameters:

a - the first collection, must not be null

b - the second collection, must not be null Returns: the union of the two collections

See Also: Collection.addAll(java.util.Collection)

This is probably what you need. But to do left and right, I think what you'd use is this:

subtract

public static java.util.Collection subtract(java.util.Collection a, java.util.Collection b)

Returns a new Collection containing a - b. The cardinality of each element e in the returned Collection will be the cardinality of e in a minus the cardinality of e in b, or zero, whichever is greater.

Parameters:

a - the collection to subtract from, must not be null

b - the collection to subtract, must not be null Returns: a new collection with the results

See Also: Collection.removeAll(java.util.Collection)

Upvotes: 5

Related Questions