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 types, I want to build a third collection whose elements are all the possible Pairs of elements from both collections. Basically:
The two distinct element types...
public class A {}
public class B {}
A "pair" of As and Bs.
public class Pair {
private A a;
private B b;
public Pair(A a, B b){
this a = a;
this b = b;
}
}
The "combination" made using old-style java.util.Collection
API:
public Collection<Pair> combine(Collection<A> as, Collection<B> bs){
Collection<Pair> pairs = new ArrayList();
foreach(A a: as){
foreach(B b: bs){
Pair pair = new Pair(a,b);
pairs.add(pair);
}
}
return pairs;
}
The ordering in the resulting pairs collection is not important. So, every instance of Pair could be created and added to the resulting collection in parallel. How could I achieve this?
The best I could figure out by myself was to use the Streams version of foreach
:
as.foreach(
a -> {
bs.foreach(
b -> {
Pair pair = new Pair(a,b);
pairs.add(pair);
}
}
);
This example was made trivial for the sake of simplification. The class Pair
is an example of processing two elements into a third one (that is, a java.util.function.BiFunction
), and adding them to a Collection
is just an example of a mutable reduction.
Is there a more elegant way to do that? Or preferable, in a more profitable way in regards to efficiency? Something like
BiFunction<A,B,Pair> combinator = Pair::new; //or any other function f(a,b)=c;
Stream<Pair> pairStream =
Streams.unknownElegantMethod(as.stream(), bs.stream(), combinator);
(a,b) -> {return new Pair(a,b)}; use
Pair::new. And for the question, stream are usable once. And you Java code shows that you are using a stream more than one time (the one on
bs`).Pair:new
, I've updated the question. Second point is mistaken, asbs
is aCollection
, and not a stream.Stream<Pair> pairStream = Streams.unknownElegantMethod(as.stream(), bs.stream(), combinator);
if, and only if, you want all possible pairs of (A, B). The use offlatMap
(as in the answer), orforeach
(as in your code), is the only solution.