Java Utililty Methods Stream Operation

List of utility methods to do Stream Operation

Description

The list of methods to do Stream Operation are organized into topic(s).

Method

booleanstartsWith(Stream stream, Iterable iterable)
   assertTrue(StreamUtils.startsWith(Stream.of(1,2,3,4),Arrays.asList(1,2,3))); 
return startsWith(stream, iterable.iterator());
booleanstartsWith(Stream stream, Stream stream2)
starts With
return startsWith(stream, stream2.iterator());
StreamstringPositions(StringBuilder toTest, Stream strings)
Tests the supplied StringBuilder for the presence of strings , and returns the positions of matching characters.
if (toTest == null) {
    throw new IllegalArgumentException("StringBuilder must not be null.");
if (strings == null) {
    throw new IllegalArgumentException("Invalid strings must not be null");
Stream.Builder<Integer> positionStream = Stream.builder();
strings.forEach(invalidString -> {
...
ListtoList(final Stream stream)
to List
return stream.collect(Collectors.toList());
StreamtoParameter(Stream input, Class exp)
to Parameter
return input.map(s -> new Object[] { s, exp });
StringtoString(IntStream s)
to String
return s.mapToObj(String::valueOf).collect(Collectors.joining());
StringtoString(LongStream ls)
Consumes all the values provided by the passed LongStream, displaying them as an array of values (on a single line).
StringBuilder sb = new StringBuilder("[");
ls.peek(sb::append).peek(n -> sb.append(", ")).filter(n -> false).findAny();
String output = sb.substring(0, sb.length() - 2);
return output + "]";
StreamupcastStream(Stream stream)
upcast Stream
return (Stream<T>) stream;
Stream>zip(List> streams)
Convert a List of Stream into a Stream of List by associating each element of one list with the element at the same position in the latters.
final List<Iterator<T>> iteratorsList = streams.stream().map(Stream::iterator).collect(Collectors.toList());
final Iterator<List<T>> zippedIterator = new Iterator<List<T>>() {
    @Override
    public boolean hasNext() {
        for (Iterator<T> it : iteratorsList)
            if (it.hasNext())
                return true;
        return false;
...
Stream>zipWithIndex(Stream stream, int startIndex)
Zips the specified stream with its indices.
return iterate(new Iterator<Map.Entry<Integer, T>>() {
    private final Iterator<? extends T> streamIterator = stream.iterator();
    private int index = startIndex;
    @Override
    public boolean hasNext() {
        return streamIterator.hasNext();
    @Override
...