JAVA8 Collector接口
Collector接口如下
public interface Collector<T, A, R> {
Supplier<A> supplier();
BiConsumer<A, T> accumulator();
BinaryOperator<A> combiner();
Function<A, R> finisher();
Set<Characteristics> characteristics();
}
<T,A,R> 的意义:
T是供应源 A是累加器 R是组合器
一、例子一:
自己写 toList() 的实现
public class ToCollectionList<T> implements Collector<T, List<T>, List<T>>{
@Override
//供应源--建立新的结果容器
public Supplier<List<T>> supplier() {
return ArrayList::new;
}
@Override
//累加器--将元素添加到结果容器中
public BiConsumer<List<T>, T> accumulator() {
return List::add;
}
@Override
//组合器--合并两个结果容器
public BinaryOperator<List<T>> combiner() {
return (listA, listB) -> {
listA.addAll(listB);
return listA;
};
}
@Override
//恒等函数--对结果容器应用转换
public Function<List<T>, List<T>> finisher() {
return Function.identity();
}
@Override
//返回不可变Characteristics,定义收集器的行为,尤其是是否进行并行归约
public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
}
}
二、例子二:
比自带partitioningBy 性能更强的 分区实现(质数)
public class FuckPrime implements Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> {
@Override
public Supplier<Map<Boolean, List<Integer>>> supplier() {
return () -> new HashMap<Boolean, List<Integer>>() {
private static final long serialVersionUID = -2542842140223455161L;
{
put(true, new ArrayList<Integer>());
put(false, new ArrayList<Integer>());
}
};
}
@Override
public BiConsumer<Map<Boolean, List<Integer>>, Integer> accumulator() {
return (Map<Boolean, List<Integer>> map, Integer can) -> {
map.get(isPrime(map.get(true), can)).add(can);
};
}
@Override
public BinaryOperator<Map<Boolean, List<Integer>>> combiner() {
return (Map<Boolean, List<Integer>> map1, Map<Boolean, List<Integer>> map2) -> {
map1.get(true).addAll(map2.get(true));
map1.get(false).addAll(map2.get(false));
return map1;
};
}
@Override
public Function<Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> finisher() {
return Function.identity();
}
@Override
public Set<java.util.stream.Collector.Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
}
private static boolean isPrime(List<Integer> list, int can) {
int canRoot = (int) Math.sqrt((double) can);
return takeWhile(list, i -> i <= canRoot).stream().noneMatch(p -> can % p == 0);
}
private static boolean isPrime2(int num) {
int num2 = (int) Math.sqrt((double) num);
return IntStream.rangeClosed(2, num2).noneMatch(t -> num % t == 0);
}
private static <T> List<T> takeWhile(List<T> list, Predicate<T> p) {
int i = 0;
for (T t : list) {
if (!p.test(t)) {
return list.subList(0, i);
}
i++;
}
return list;
}
public static Map<Boolean, List<Integer>> getPrime(int n) {
return IntStream.rangeClosed(2, n).boxed().collect(new FuckPrime());
}
public static Map<Boolean, List<Integer>> getPrime2(int n) {
return IntStream.rangeClosed(2, n).boxed().collect(Collectors.partitioningBy(i -> isPrime2(i)));
}
public static void main(String[] args) {
Long s = System.nanoTime();
for (int i = 0; i <= 10; i++) {
getPrime(1_000_000);
}
Long e = System.nanoTime();
System.out.println("自写方法耗时:" + (e - s) / 1_000_000 + "ms");
Long s2 = System.nanoTime();
for (int i = 0; i <= 10; i++) {
getPrime2(1_000_000);
}
Long e2 = System.nanoTime();
System.out.println("Stream分区方法耗时:" + (e2 - s2) / 1_000_000 + "ms");
}
}
10次1000000内搜索结果:
自写方法耗时:3015ms
Stream分区方法耗时:4785ms