@Test void zipWithIndex() { Stream<Tuple2<String, Long>> result = StreamUtil.zipWithIndex(Stream.of("a", "b", "c")); assertThat(result, contains(Tuple.of("a", 0L), Tuple.of("b", 1L), Tuple.of("c", 2L))); }
public static <T> Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream) { Iterator<Tuple2<T,Long>> iterator = new Iterator<Tuple2<T,Long>>() { private final Iterator<? extends T> streamIterator = stream.iterator(); private long index = 0; @Override public boolean hasNext() { return streamIterator.hasNext(); } @Override public Tuple2<T,Long> next() { return Tuple.of(streamIterator.next(), index++); } }; return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false); }
StreamUtil extends UtilityClass { public static <T> Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream) { Iterator<Tuple2<T,Long>> iterator = new Iterator<Tuple2<T,Long>>() { private final Iterator<? extends T> streamIterator = stream.iterator(); private long index = 0; @Override public boolean hasNext() { return streamIterator.hasNext(); } @Override public Tuple2<T,Long> next() { return Tuple.of(streamIterator.next(), index++); } }; return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false); } }
StreamUtil extends UtilityClass { public static <T> Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream) { Iterator<Tuple2<T,Long>> iterator = new Iterator<Tuple2<T,Long>>() { private final Iterator<? extends T> streamIterator = stream.iterator(); private long index = 0; @Override public boolean hasNext() { return streamIterator.hasNext(); } @Override public Tuple2<T,Long> next() { return Tuple.of(streamIterator.next(), index++); } }; return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false); }  }
StreamUtil extends UtilityClass { public static <T> Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream) { Iterator<Tuple2<T,Long>> iterator = new Iterator<Tuple2<T,Long>>() { private final Iterator<? extends T> streamIterator = stream.iterator(); private long index = 0; @Override public boolean hasNext() { return streamIterator.hasNext(); } @Override public Tuple2<T,Long> next() { return Tuple.of(streamIterator.next(), index++); } }; return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false); }  static Stream<T> ofNullable(T value); static Stream<T> ofBlankable(T value); static Stream<T> of(Optional<T> opt); static Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream); static Stream<R> mapWithIndex(Stream<T> input, BiFunction<? super T, Long, ? extends R> mapping); static byte[] toByteArray(IntStream stream); }
StreamUtil extends UtilityClass { public static <T> Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream) { Iterator<Tuple2<T,Long>> iterator = new Iterator<Tuple2<T,Long>>() { private final Iterator<? extends T> streamIterator = stream.iterator(); private long index = 0; @Override public boolean hasNext() { return streamIterator.hasNext(); } @Override public Tuple2<T,Long> next() { return Tuple.of(streamIterator.next(), index++); } }; return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE), false); }  static Stream<T> ofNullable(T value); static Stream<T> ofBlankable(T value); static Stream<T> of(Optional<T> opt); static Stream<Tuple2<T, Long>> zipWithIndex(Stream<? extends T> stream); static Stream<R> mapWithIndex(Stream<T> input, BiFunction<? super T, Long, ? extends R> mapping); static byte[] toByteArray(IntStream stream);  }