java parallel stream splitter

Further to the issue with default JDK implementation with IteratorSpliterator,

I have created a custom Spliterator to evenly split the stream and with threshold for the split size.

public class CacheSplitter<T> implements Spliterator<T> {
//    private final Collection<T> collection;
    private final T[] values;
    private int start;
    private int end;
    private final int THRESHOLD;
    public CacheSplitter(T[] values, int threshold) {
        this(values, 0, values.length, threshold);
    public CacheSplitter(T[] values, int start, int end, int threshold) {
        this.values = values;
        this.start = start;
        this.end = end;
        this.THRESHOLD = threshold;

    public boolean tryAdvance(Consumer action) {
        if(start< end){
            return true;
        return false;

    public Spliterator trySplit() {
        if(end - start < THRESHOLD){
            return null;
        int mid = (start + end)/2;
        return new CacheSplitter(values, start, start= mid+1, THRESHOLD);

    public long estimateSize() {
        return end - start;

    public int characteristics() {

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s