JAVA8 Collector接口掠过

JAVA8 Collector接口

Collector接口如下
public interface Collector {
    Supplier supplier();
    BiConsumer accumulator();
    BinaryOperator combiner();
    Function finisher();
    Set characteristics();
}

 的意义:

T是供应源 A是累加器 R是组合器
一、例子一:

自己写 toList() 的实现

public class ToCollectionList implements Collector, List>{
    @Override
    //供应源--建立新的结果容器
    public Supplier> supplier() {
        return ArrayList::new;
    }
    @Override
    //累加器--将元素添加到结果容器中
    public BiConsumer, T> accumulator() {
        return List::add;
    }
    @Override
    //组合器--合并两个结果容器
    public BinaryOperator> combiner() {
        return (listA, listB) -> {
            listA.addAll(listB);
            return listA;
        };
    }
    @Override
    //恒等函数--对结果容器应用转换
    public Function, List> finisher() {
        return Function.identity();
    }
    @Override
    //返回不可变Characteristics,定义收集器的行为,尤其是是否进行并行归约
    public Set characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
    }
}
二、例子二:

比自带partitioningBy 性能更强的 分区实现(质数)

public class FuckPrime implements Collector>, Map>> {

    @Override
    public Supplier>> supplier() {
        return () -> new HashMap>() {

            private static final long serialVersionUID = -2542842140223455161L;

            {
                put(true, new ArrayList());
                put(false, new ArrayList());
            }
        };
    }

    @Override
    public BiConsumer>, Integer> accumulator() {
        return (Map> map, Integer can) -> {
            map.get(isPrime(map.get(true), can)).add(can);
        };
    }

    @Override
    public BinaryOperator>> combiner() {
        return (Map> map1, Map> map2) -> {
            map1.get(true).addAll(map2.get(true));
            map1.get(false).addAll(map2.get(false));
            return map1;
        };
    }

    @Override
    public Function>, Map>> finisher() {
        return Function.identity();
    }

    @Override
    public Set characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }

    private static boolean isPrime(List 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  List takeWhile(List list, Predicate p) {
        int i = 0;
        for (T t : list) {
            if (!p.test(t)) {
                return list.subList(0, i);
            }
            i++;
        }
        return list;
    }

    public static Map> getPrime(int n) {
        return IntStream.rangeClosed(2, n).boxed().collect(new FuckPrime());
    }

    public static Map> 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

你可能感兴趣的:(JAVA8 Collector接口掠过)