Collectors - The Collector interface
原创转载请注明出处:http://agilestyle.iteye.com/blog/2425967
The Collector Interface
public interface Collector<T, A, R> {
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
BinaryOperator<A> combiner();
Function<A, R> finisher();
Set<Collector.Characteristics> characteristics();
}
Note:
T is the generic type of the items in the stream to be collected.
A is the type of the accumulator, the object on which the partial result will be accumulated during the collection process.
R is the type of the object (typically, but not always, the collection) resulting from the collect operation.
实现一个自定义的 ToListCollector<T>
Making a new result container: the supplier method
@Override
public Supplier<List<T>> supplier() {
return ArrayList::new;
}
Adding an element to a result container: the accumulator method
@Override
public BiConsumer<List<T>, T> accumulator() {
return List::add;
}
Applying the final transformation to the result container: the finisher method
@Override
public Function<List<T>, List<T>> finisher() {
return Function.identity();
}
Logical steps of the sequential reduction process

Merging two result containers: the combiner method
@Override
public BinaryOperator<List<T>> combiner() {
return (list1, list2) -> {
list1.addAll(list2);
return list1;
};
}
Parallelizing the reduction process using the combiner method

Characteristics method
characteristics, returns an immutable set of Characteristics, defining the behavior of the collector—in particular providing hints about whether the stream can be reduced in parallel and which optimizations are valid when doing so. Characteristics is an enumeration containing three items:
- UNORDERED—The result of the reduction isn’t affected by the order in which the items in the stream are traversed and accumulated.
- CONCURRENT—The accumulator function can be called concurrently from multiple threads, and then this collector can perform a parallel reduction of the stream. If the collector isn’t also flagged as UNORDERED, it can perform a parallel reduction only when it’s applied to an unordered data source.
- IDENTITY_FINISH—This indicates the function returned by the finisher method is the identity one, and its application can be omitted. In this case, the accumulator object is directly used as the final result of the reduction process. This also implies that it’s safe to do an unchecked cast from the accumulator A to the result R.
@Override
public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
}
Putting them all together
package org.fool.java8.collector;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
private void log(String log) {
System.out.println(Thread.currentThread().getName() + "-" + log);
}
@Override
public Supplier<List<T>> supplier() {
log("supplier");
return ArrayList::new;
}
@Override
public BiConsumer<List<T>, T> accumulator() {
log("accumulator");
return List::add;
}
@Override
public BinaryOperator<List<T>> combiner() {
log("combiner");
return (list1, list2) -> {
list1.addAll(list2);
return list1;
};
}
@Override
public Function<List<T>, List<T>> finisher() {
log("finisher");
return Function.identity();
}
@Override
public Set<Characteristics> characteristics() {
log("characteristics");
//return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
}
}
Test.java
package org.fool.java8.collector;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collector;
public class Test {
public static void main(String[] args) {
Collector<String, List<String>, List<String>> collector = new ToListCollector<>();
String[] strings = {"Hello", "World", "Java 8", "Lambda", "Collector"};
Optional.of(Arrays.stream(strings).filter(s -> s.length() > 5).collect(collector)).ifPresent(System.out::println);
}
}
Console Output
main-supplier
main-accumulator
main-combiner
main-characteristics
main-characteristics
[Java 8, Lambda, Collector]
Reference
Manning.Java.8.in.Action.Lambdas.Streams.and.functional-style.programming