Java Stream Operation startsWith(Stream stream, Stream stream2)

Here you can find the source of startsWith(Stream stream, Stream stream2)

Description

starts With

License

Apache License

Declaration

public final static <T> boolean startsWith(Stream<T> stream, Stream<T> stream2) 

Method Source Code

//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;

    }
}

Related

  1. replacePos(StringBuilder original, char replacementChar, Stream positionsToReplace)
  2. replaceStr(StringBuilder original, char replacementChar, Stream stringsToReplace)
  3. reverse(Stream stream)
  4. reverse(Stream input)
  5. startsWith(Stream stream, Iterable iterable)
  6. stringPositions(StringBuilder toTest, Stream strings)
  7. toList(final Stream stream)
  8. toParameter(Stream input, Class exp)
  9. toString(IntStream s)