I need to do some Matrix work in an efficient and flexible way and hoped I could practice my Java 8 using streams and lambdas and perhaps even get free parallelism out of it. One point I am struggling with is how to perform an operation on two streams putting the result into a third.
Consider the simple mechanism:
static final List<String> a = Arrays.asList("A", "A", "A");
static final List<String> b = Arrays.asList("B", "B", "B");
public void withoutStreams() {
// The boring old way.
List<String> c = new ArrayList<>();
for (Iterator<String> ai = a.iterator(), bi = b.iterator(); ai.hasNext() && bi.hasNext();) {
c.add(ai.next() + bi.next());
}
System.out.println(c);
}
Works fine but I want to use Streams.
private void withStreams() {
List<String> c = new ArrayList<>();
combine(a.stream(), b.stream(), c, (String x, String y) -> x + y);
}
private void combine(Stream<String> a, Stream<String> b, List<String> c, BinaryOperator<String> op) {
// What can I do here to make it happen?
}
I fully expect we will be filling c
using a Consumer
of some form but extra kudos for coming up with some way of referring to a specific cell of the matrix other than using (row,col) bearing in mind that the cells will be immutable.