I want to execute two streams simultaneously and generate a third stream as a combination of the first two.Suppose I have the following two streams
Stream<String> a = Stream.of("f","b","z");
Stream<String> b = Stream.of("foo","bar","baz");
and I want to generate a new one from these two which would be equivalent to this one
Stream<String> c = Stream.of("f:foo","b:bar","z:baz");
What I have found so far is Stream.concat
or Stream.of
Stream<String> result = Stream.concat(a, b); or
Stream<String> result = Stream.of(a, b).flatMap(s -> s);
where both are rather attached to each other and correspond to this here
Stream.of("f","b","z","foo","bar","baz");
So far my workaround is to collect the elements of the two streams to one list each and iterate over both lists to concatenate the single elements and turn the result list into a stream. Is there a shorter way or an existing function in strem api to work simultaneously over two streams?
List<String> aList = a.collect(Collectors.toList());
List<String> bList = b.collect(Collectors.toList());
List<String> result = new ArrayList<>();
for(int i = 0; i< aList.size(); i++){
result.add(aList.get(i)+":"+bList.get(i));
}
Stream<String> cc = result.stream();