CyclicSpliterator implements Spliterator<T> { private ConcatSpliterator<T> spliterator() { if (_concat == null) { _concat = new ConcatSpliterator<>( _spliterators.stream() .map(Supplier::get) .collect(Collectors.toList()) ); } return _concat; } CyclicSpliterator(final List<Supplier<Spliterator<T>>> spliterators); @Override boolean tryAdvance(final Consumer<? super T> action); @Override Spliterator<T> trySplit(); @Override long estimateSize(); @Override int characteristics();  }
@Test public void cycle1() { final Supplier<Spliterator<Integer>> s1 = () -> Stream.of(1, 2, 3).spliterator(); final Supplier<Spliterator<Integer>> s2 = () -> Stream.of(4, 5, 6).spliterator(); final Supplier<Spliterator<Integer>> s3 = () -> Stream.of(7, 8, 9).spliterator(); final CyclicSpliterator<Integer> s = new CyclicSpliterator<>(List.of(s1, s2, s3)); final int[] array = StreamSupport.stream(s, false) .limit(10) .mapToInt(Integer::intValue) .toArray(); Assert.assertEquals(array, new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 }); }
@Test public void cycle2() { final Supplier<Spliterator<Integer>> s1 = () -> Stream.of(1, 2, 3).spliterator(); final Supplier<Spliterator<Integer>> s2 = () -> Stream.of(4, 5, 6).spliterator(); final Supplier<Spliterator<Integer>> s3 = () -> Stream.of(7, 8, 9).spliterator(); final CyclicSpliterator<Integer> s = new CyclicSpliterator<>(List.of(s1, s2, s3)); final int[] array = StreamSupport.stream(s, false) .limit(35) .mapToInt(Integer::intValue) .toArray(); Assert.assertEquals(array, new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 }); }