Here you can find the source of endsWith(Stream
public final static <T> boolean endsWith(Stream<T> stream, Iterable<T> iterable)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Spliterator; import java.util.Spliterators; import java.util.stream.Stream; import java.util.stream.StreamSupport; public class Main { public final static <T> boolean endsWith(Stream<T> stream, Iterable<T> iterable) { Iterator<T> it = iterable.iterator(); List<T> compare1 = new ArrayList<>(); while (it.hasNext()) { compare1.add(it.next());/*from w ww . j a v a 2 s . co m*/ } LinkedList<T> list = new LinkedList<>(); stream.forEach(v -> { list.add(v); if (list.size() > compare1.size()) list.remove(); }); return startsWith(list.stream(), compare1.iterator()); } /** * * <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()); } /** * <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; } /** * Create a stream from an iterable * <pre> * {@code * assertThat(StreamUtils.stream(Arrays.asList(1,2,3)) * .collect(Collectors.toList()), * equalTo(Arrays.asList(1,2,3))); * * } * </pre> * @param it Iterable to convert to a Stream * @return Stream from iterable */ public static <U> Stream<U> stream(Iterable<U> it) { return StreamSupport.stream(it.spliterator(), false); } public static <U> Stream<U> stream(Spliterator<U> it) { return StreamSupport.stream(it, false); } /** * Create a stream from an iterator * <pre> * {@code * assertThat(StreamUtils.stream(Arrays.asList(1,2,3).iterator()) * .collect(Collectors.toList()), * equalTo(Arrays.asList(1,2,3))); * } * </pre> * @param it Iterator to convert to a Stream * @return Stream from iterator */ public static <U> Stream<U> stream(Iterator<U> it) { return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false); } /** * Create a stream from a map * <pre> * {@code * Map<String,String> map = new HashMap<>(); map.put("hello","world"); assertThat(StreamUtils.stream(map).collect(Collectors.toList()),equalTo(Arrays.asList(new AbstractMap.SimpleEntry("hello","world")))); * }</pre> * * * @param it Iterator to convert to a Stream * @return Stream from a map */ public final static <K, V> Stream<Map.Entry<K, V>> stream(Map<K, V> it) { return it.entrySet().stream(); } }