Java Stream Operation lazyPartition(final Stream stream, final int maxPartitionSize)

Here you can find the source of lazyPartition(final Stream stream, final int maxPartitionSize)

Description

lazy Partition

License

Apache License

Declaration

public static <T> Stream<Stream<T>> lazyPartition(final Stream<T> stream, final int maxPartitionSize) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Main {
    public static <T> Stream<Stream<T>> lazyPartition(final Stream<T> stream, final int maxPartitionSize) {
        final Iterator<T> iterator = stream.iterator();
        final Iterable<Stream<T>> partitionedIterable = () -> new Iterator<Stream<T>>() {
            public boolean hasNext() {
                return iterator.hasNext();
            }/*  ww w. j a  v  a  2 s. c o  m*/

            public Stream<T> next() {
                List<T> partition = new ArrayList<>();
                while (partition.size() < maxPartitionSize && iterator.hasNext()) {
                    partition.add(iterator.next());
                }
                return partition.stream();
            }
        };
        return StreamSupport.stream(partitionedIterable.spliterator(), false);
    }
}

Related

  1. isEmpty(final Stream stream)
  2. isFinite(Stream stream)
  3. iterableOf(Stream stream)
  4. iteratorToFiniteStream(Iterator iterator, boolean parallel)
  5. iteratorToStream(final Iterator iterator)
  6. lazyStream(PrimitiveIterator.OfLong iterator)
  7. longArray(Stream stream)
  8. longList(LongStream stream)
  9. makeStream(final Object[] array)