Here you can find the source of startsWith(Stream
public final static <T> boolean startsWith(Stream<T> stream, Stream<T> stream2)
//package com.java2s; //License from project: Apache License import java.util.Iterator; import java.util.Objects; import java.util.stream.Stream; public class Main { /**/*from w ww .j a va 2s . c o m*/ * * <pre>{@code * assertTrue(StreamUtils.startsWith(Stream.of(1,2,3,4),Arrays.asList(1,2,3))); * }</pre> * * @param iterable * @return True if Monad starts with Iterable sequence of data */ public final static <T> boolean startsWith(Stream<T> stream, Iterable<T> iterable) { return startsWith(stream, iterable.iterator()); } public final static <T> boolean startsWith(Stream<T> stream, Stream<T> stream2) { return startsWith(stream, stream2.iterator()); } /** * <pre> * {@code * assertTrue(StreamUtils.startsWith(Stream.of(1,2,3,4),Arrays.asList(1,2,3).iterator())) * }</pre> * @param iterator * @return True if Monad starts with Iterators sequence of data */ public final static <T> boolean startsWith(Stream<T> stream, Iterator<T> iterator) { Iterator<T> it = stream.iterator(); while (iterator.hasNext()) { if (!it.hasNext()) return false; if (!Objects.equals(it.next(), iterator.next())) return false; } return true; } }