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);
I hope I don't have any silly typos, but basically what you can do is :
List<Pair> list = as
.stream()
.flatMap(a -> bs.stream().map (b -> new Pair(a,b)))
.collect (Collectors.toList());
Stream<A>
from as
.a
instanceStream<B>
of bs
b
to a pair of (a,b)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With