JAVA 8 Collectorインタフェースを略す

4696 ワード

JAVA 8 Collectorインタフェース
Collectorインタフェースは次のとおりです.
public interface Collector {
    Suppliersupplier();
BiConsumeraccumulator();
BinaryOperatorcombiner();
Functionfinisher();
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