Understanding Spliterator, Collector and Stream in Java 8

后端 未结 4 1018
栀梦
栀梦 2020-12-12 10:33

I am having trouble understanding the Stream interface in Java 8, especially where it has to do with the Spliterator and Collector interfaces. My problem is that I simply ca

4条回答
  •  误落风尘
    2020-12-12 11:26

    Spliterator basically means "splittable Iterator".

    Single thread can traverse/process the entire Spliterator itself, but the Spliterator also has a method trySplit() which will "split off" a section for someone else (typically, another thread) to process -- leaving the current spliterator with less work.

    Collector combines the specification of a reduce function (of map-reduce fame), with an initial value, and a function to combine two results (thus enabling results from Spliterated streams of work, to be combined.)

    For example, the most basic Collector would have an initial vaue of 0, add an integer onto an existing result, and would 'combine' two results by adding them. Thus summing a spliterated stream of integers.

    See:

    • Spliterator.trySplit()
    • Collector

提交回复
热议问题