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

igorp1024 :

For instance there are collections [1,2,3,4,5], [6,7,8], [9,0]. Any way to avoid loops with iterators to interleave these collections via Java 8 stream API to get the following result - [1,6,9,2,7,0,3,8,4,5]?

Tunaki :

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)
                    .boxed()
                    .flatMap(i -> lists.stream().filter(l -> i < l.size()).map(l -> l.get(i)))
                    .collect(Collectors.toList());
}

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());
System.out.println(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

BULK COLLECT INTO multiple collections

is it possible collect multiple flows in one

Is there a way to spread data evenly among multiple mongodb collections?

Is it possible to collect a stream to two different collections using one line?

Is it possible to collect multiple inputs in one getche() function?

Javascript - Collect data from mongodb from multiple collections then merge into array

Is it possible to initialize multiple lazy collections in parallel?

Mongoose populate ObjectID from multiple possible collections

Is it possible to coalesce Spark partitions "evenly"?

Is it possible to query multiple document sub-collections in Cloud Firestore?

Distribute an integer amount by a set of slots as evenly as possible

How to randomly split a map in Go as evenly as possible?

How to distribute a number to an array as evenly as possible?

Is it possible to build an evenly spaced and unconstrained grid in Compose?

MongoDB sharding is possible on collections?

Collect multiple Flux into one

Collect multiple RequireJS dependencies

Bulk collect into multiple columns

Evenly space multiple views within a container view

Multiple averages over evenly spaced intervals

Resize multiple items inside of a WxPython BoxSizer evenly?

It is possible to collect a &mut from an iterator?

spark avoid collect as much as possible

Is it possible to use a combiner function when flatMap in Java?

Is std::iter::FlatMap.clone() possible?

Split a list into sublists in java using if possible flatMap

Java – Combine Multiple Collections

Mongodb search multiple collections

GORM Querying multiple collections