igorp1024 igorp1024 - 1 year ago 66
Java Question

Is it possible to .flatMap() or .collect() evenly multiple collections

For instance there are collections

. Any way to avoid loops with iterators to interleave these collections via Java 8 stream API to get the following result -

Answer Source

I am not sure if there's a simpler way with the Stream API, but you can do this using a stream over the indices of all the lists to consider:

static <T> List<T> interleave(List<List<T>> lists) {
    int maxSize = lists.stream().mapToInt(List::size).max().orElse(0);
    return IntStream.range(0, maxSize)
                    .flatMap(i -> lists.stream().filter(l -> i < l.size()).map(l -> l.get(i)))

This gets the size of the greatest list in the given lists. For each index, it then flat maps it with a stream formed by the elements of each list at that index (if the element exist).

Then you can use it with

public static void main(String[] args) {
    List<Integer> list1 = Arrays.asList(1,2,3,4,5);
    List<Integer> list2 = Arrays.asList(6,7,8);
    List<Integer> list3 = Arrays.asList(9,0);
    System.out.println(interleave(Arrays.asList(list1, list2, list3))); // [1, 6, 9, 2, 7, 0, 3, 8, 4, 5]

Using the protonpack library, you can use the method interleave to do just that:

List<Stream<Integer>> lists = Arrays.asList(list1.stream(), list2.stream(), list3.stream());
List<Integer> result = StreamUtils.interleave(Selectors.roundRobin(), lists).collect(Collectors.toList());