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是供應(yīng)源 A是累加器 R是組合器
一、例子一:
自己寫 toList() 的實現(xiàn)
public class ToCollectionList<T> implements Collector<T, List<T>, List<T>>{
@Override
//供應(yīng)源--建立新的結(jié)果容器
public Supplier<List<T>> supplier() {
return ArrayList::new;
}
@Override
//累加器--將元素添加到結(jié)果容器中
public BiConsumer<List<T>, T> accumulator() {
return List::add;
}
@Override
//組合器--合并兩個結(jié)果容器
public BinaryOperator<List<T>> combiner() {
return (listA, listB) -> {
listA.addAll(listB);
return listA;
};
}
@Override
//恒等函數(shù)--對結(jié)果容器應(yīng)用轉(zhuǎn)換
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 性能更強的 分區(qū)實現(xiàn)(質(zhì)數(shù))
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分區(qū)方法耗時:" + (e2 - s2) / 1_000_000 + "ms");
}
}
10次1000000內(nèi)搜索結(jié)果:
自寫方法耗時:3015ms
Stream分區(qū)方法耗時:4785ms