I have this situation where it seems that the Java 8 Streams API would be helpful, but I\'m not fully sure how it could be.
From two Collections with distinct element ty
If you're open to using a third-party library, you could use Eclipse Collections Sets.cartesianProduct(). This would require that your a's and b's are both Sets. Eclipse Collections has a Pair type built-in, so you wouldn't need to create it.
public class A {}
public class B {}
public List> combine(Set as, Set bs)
{
return Sets.cartesianProduct(as, bs).toList();
}
If your a's and b's are not Sets, then you could use a CollectionAdapter flatCollect and collect, which are equivalent to flatMap and map on Stream.
public Collection> combine(Collection as, Collection bs)
{
MutableCollection adaptB = CollectionAdapter.adapt(bs);
return CollectionAdapter.adapt(as)
.flatCollect(a -> adaptB.asLazy().collect(b -> Tuples.pair(a, b)));
}
Another possible option using Stream would be to define your own Collector for cartesianProduct. This is more complex than the other Stream solution, and would only be useful if you used cartesianProduct a few times in your code.
List> pairs = as.stream().collect(cartesianProduct(bs));
public static Collector>>
cartesianProduct(Collection other)
{
return Collector.of(
ArrayList::new,
(list, a) -> list.addAll(
other.stream().map(b -> new Pair(a, b))).collect(Collectors.toList())),
(list1, list2) ->
{
list1.addAll(list2);
return list1;
},
Collector.Characteristics.UNORDERED
);
}
Note: I am a committer for Eclipse Collections.