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]
?
问题:
回答1:
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);
回答2:
Not a very good solution though. Just a try
List<String> listA = Arrays.asList("a1","a2","a3","a4");
List<String> listB = Arrays.asList("b1","b2","b3","b4");
List<String> listC = Arrays.asList("c1","c2","c3","c4");
List<String> combined = Stream.of(listA, listB, listC).flatMap(Collection::stream).collect(Collectors.toList());
AtomicInteger index = new AtomicInteger();
List<String> list1 = combined
.stream().filter(x -> index.incrementAndGet() % 4 ==1).collect(Collectors.toList());
index.set(0);
List<String> list2 = combined
.stream().filter(x -> index.incrementAndGet() % 4 ==2).collect(Collectors.toList());
index.set(0);
List<String> list3 = combined
.stream().filter(x -> index.incrementAndGet() % 4 ==3).collect(Collectors.toList());
index.set(0);
List<String> list0 = combined
.stream().filter(x -> index.incrementAndGet() % 4 ==0).collect(Collectors.toList());
List<String> desiredOutput = Stream.of(list1, list2, list3,list0).flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(String.join(",", desiredOutput));
回答3:
I like Tunaki’s answer and have been working a bit more on it:
static <T> List<T> interleave(List<? extends Collection<T>> lists) {
int maxSize = lists.stream().mapToInt(Collection::size).max().orElse(0);
List<Iterator<T>> iterators = lists.stream().map(l -> l.stream().iterator()).collect(Collectors.toList());
return IntStream.range(0, maxSize)
.boxed()
.flatMap(i -> iterators.stream().filter(it -> it.hasNext()).map(it -> it.next()))
.collect(Collectors.toList());
}
I wanted to see if we can treat the initial lists as streams too, it turned out we can. As a side effect, we avoid get(i)
on a list (if that was really a very long LinkedList
, we would want to avoid it). I didn’t find a way to do without knowledge of a max length of the original lists.