说到limit,不得不提及一下另外一个流操作:sorted。该操作用于对流中元素进行排序,sorted要求待比较的元素必须实现Comparable接口,如果没有实现也不要紧,我们可以将比较器作为参数传递给sorted(Comparator<? super T> comparator),比如我们希望筛选出专业为土木工程的学生,并按年龄从小到大排序,筛选出年龄最小的两个学生,那么可以实现为:
除了上面这类基础的map,java8还提供了mapToDouble(ToDoubleFunction<? super T> mapper),mapToInt(ToIntFunction<? super T> mapper),mapToLong(ToLongFunction<? super T> mapper),这些映射分别返回对应类型的流,java8为这些流设定了一些特殊的操作,比如我们希望计算所有专业为计算机科学学生的年龄之和,那么我们可以实现如下:
int totalAge = students.stream()
.filter(student -> "计算机科学".equals(student.getMajor()))
.mapToInt(Student::getAge).sum();
public interface Collector<T, A, R> {
/**
* A function that creates and returns a new mutable result container.
*
* @return a function which returns a new, mutable result container
*/
Supplier<A> supplier();
</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">
* A function that folds a value into a mutable result container.
*
* </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> a function which folds a value into a mutable result container
</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">
BiConsumer</span><A, T><span style="color: rgb(0, 0, 0);"> accumulator();
</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">
* A function that accepts two partial results and merges them. The
* combiner function may fold state from one argument into the other and
* return that, or may return a new result container.
*
* </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> a function which combines two partial results into a combined
* result
</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">
BinaryOperator</span><A><span style="color: rgb(0, 0, 0);"> combiner();
</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">
* Perform the final transformation from the intermediate accumulation type
* {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> A} to the final result type {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> R}.
*
* <p>If the characteristic {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> IDENTITY_TRANSFORM} is
* set, this function may be presumed to be an identity transform with an
* unchecked cast from {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> A} to {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> R}.
*
* </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> a function which transforms the intermediate result to the final
* result
</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">
Function</span><A, R><span style="color: rgb(0, 0, 0);"> finisher();
</span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">
* Returns a {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> Set} of {</span><span style="color: rgb(128, 128, 128);">@code</span><span style="color: rgb(0, 128, 0);"> Collector.Characteristics} indicating
* the characteristics of this Collector. This set should be immutable.
*
* </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);"> an immutable set of collector characteristics
</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">
Set</span><Characteristics><span style="color: rgb(0, 0, 0);"> characteristics();