Spliterator Java 8-自定义实现

时间:2018-10-26 11:49:12

标签: java spliterator

我正在学习Java 8的这一功能,在发现用于并行处理生成的Spliterator的自定义类的情况下,我真的很难理解trySplit()接口的Stream方法实现。

任何人都可以通过清晰的示例帮助我编写一些好的教程吗?

2 个答案:

答案 0 :(得分:1)

  

理想的trySplit方法可以有效地(无遍历)划分其   元素精确地分成两半,可以进行平衡的并行计算。许多   背离这一理想仍然非常有效;例如,仅   近似分裂近似平衡的树,或对于树   其中叶节点可能包含一个或两个元素,   进一步拆分这些节点。但是,余额和/或   效率过低的trySplit机制通常会导致效果不佳   并行性能。

以及带有注释的方法结构

 public Spliterator<T> trySplit() {
   int lo = origin; // divide range in half
   int mid = ((lo + fence) >>> 1) & ~1; // force midpoint to be even
   if (lo < mid) { // split out left half
     origin = mid; // reset this Spliterator's origin
     return new TaggedArraySpliterator<>(array, lo, mid);
   }
   else       // too small to split
     return null;
 }

要了解更多信息,请访问https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html

答案 1 :(得分:-1)

这里是实现分隔器的示例。

public class Payment {
    private String category;
    private double amount;

    public Payment(double amount, String category) {
        this.amount = amount;
        this.category = category;
    }

    public String getCategory() {
        return category;
    }

    public double getAmount() {
        return amount;
    }
}

TrySplit实现:

import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;

public class PaymentBatchSpliterator implements Spliterator<Payment> {

    private List<Payment> paymentList;
    private int current;
    private int last;  // inclusive

    public PaymentBatchSpliterator(List<Payment> payments) {
        this.paymentList = payments;
        last = paymentList.size() - 1;
    }

    public PaymentBatchSpliterator(List<Payment> payments, int start, int last) {
        this.paymentList = payments;
        this.current = start;
        this.last = last;
    }

    @Override
    public boolean tryAdvance(Consumer<? super Payment> action) {
        if (current <= last) {
            action.accept(paymentList.get(current));
            current++;
            return true;
        }
        return false;
    }

    @Override
    public Spliterator<Payment> trySplit() {
        if ((last - current) < 100) {
            return null;
        }

        // first stab at finding a split position
        int splitPosition = current + (last - current) / 2;
        // if the categories are the same, we can't split here, as we are in the middle of a batch
        String categoryBeforeSplit = paymentList.get(splitPosition-1).getCategory();
        String categoryAfterSplit = paymentList.get(splitPosition).getCategory();

        // keep moving forward until we reach a split between categories
        while (categoryBeforeSplit.equals(categoryAfterSplit)) {
            splitPosition++;
            categoryBeforeSplit = categoryAfterSplit;
            categoryAfterSplit = paymentList.get(splitPosition).getCategory();
        }

        // safe to create a new spliterator
        PaymentBatchSpliterator secondHalf = new PaymentBatchSpliterator(paymentList,splitPosition,last);
        // reset our own last value
        last = splitPosition - 1;

        return secondHalf;
    }

    @Override
    public long estimateSize() {
        return last - current;
    }

    @Override
    public int characteristics() {
        return 0;
    }
}

这是来自GitHub Reference

的示例