自定義Collector

自定義Collector

在Java8的特性中,使用了新的API,其中就有Stream,在偶然的機(jī)會(huì)下看到思否大佬對(duì)自定義Collector寫下的文章,驚為天人。

public static <T>
Collector<T, ?, List<T>> toList() {
    return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add,
                               (left, right) -> { left.addAll(right); return left; },
                               CH_ID);
}

自定義收集器需要實(shí)現(xiàn)Collector接口,其中包含5個(gè)Function接口

package function;

import java.util.*;
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;

/**
 * @Description 自定義收集器
 * @Authror taren
 * @DATE 2019/7/5 9:24
 */
public class CustomCollectors {

public static <T> Collector<T, List<List<T>>, List<List<T>>> groupByNumber() {
    return CustomCollectors.groupByNumber(2);
}

public static <T> Collector<T, List<List<T>>, List<List<T>>> groupByNumber(int number) {
    return new NumberCustomCollector(number);
}

public static class NumberCustomCollector<T> implements Collector<T, List<List<T>>, List<List<T>>> {

    private int number;

    public NumberCustomCollector(int number) {
        this.number = number;
    }
    //傳入一個(gè)泛型 List<T> -> new ArrayList<T>
    //Supplier<T> 傳入一個(gè)泛型  得到一個(gè)泛型的類型  T get()
    @Override
    public Supplier<List<List<T>>> supplier() {
        return ArrayList::new;
    }

    //BiConsumer<T,R> -> void accept(T,R) 實(shí)現(xiàn)累加器
    @Override
    public BiConsumer<List<List<T>>, T> accumulator() {
        return (list, item) -> {
            if (list.isEmpty()) {
                list.add(this.createNewList(item));
            } else {
                List<T> last = (List<T>) list.get(list.size() - 1);
                if (last.size() < number) {
                    last.add(item);
                } else {
                    list.add(this.createNewList(item));
                }
            }
        };
    }

    //組合  R apply(T t, U u)  2個(gè)類型組合成另外一個(gè)新的類型
    @Override
    public BinaryOperator<List<List<T>>> combiner() {
        return (list1, list2) -> {
            list1.addAll(list2);
            return list1;
        };
    }

    // 用自身 就是t->t 還是本身的類型 不是t->other
    @Override
    public Function<List<List<T>>, List<List<T>>> finisher() {
        return Function.identity();
    }

    // 表示 Function.identity() 就是收集的最終類型 不再做最終的轉(zhuǎn)換
    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH));
    }

    private List<T> createNewList(T item) {
        List<T> newOne = new ArrayList<T>();
        newOne.add(item);
        return newOne;
    }
}

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    // 按照2個(gè)分組
    List<List<Integer>> twoNumberList = list.stream().collect(CustomCollectors.groupByNumber());
    // 按照5個(gè)分組
    List<List<Integer>> fiveNumberList = list.stream().collect(CustomCollectors.groupByNumber(5));

    System.out.println(twoNumberList);
    System.out.println(fiveNumberList);
}
}

Supplier是一個(gè)FunctionInterface,表達(dá)是的提供一個(gè)初始化的容器,但在這里是創(chuàng)建一個(gè)累加器,即

List list=new ArrayList<>()

BiConsumer是把一個(gè)類型的東西添加到累加器中,即是

list.add(item)

BinaryOperator是把一個(gè)累加器和另一個(gè)累加器合并到一起,即是

list.add(otherList)

Function是把一個(gè)結(jié)果轉(zhuǎn)化為另外一個(gè)結(jié)果,但是在這里是

t->t

Set<Characteristics>是一個(gè)枚舉類,在這里表示Function轉(zhuǎn)化的就是最終的結(jié)果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • [TOC] 聲明 這一系列文章旨在幫助大家理解 Collector 的執(zhí)行流程,至于實(shí)現(xiàn)的是否高效、是否優(yōu)雅、是否...
    hylexus閱讀 1,590評(píng)論 0 0
  • [TOC] 聲明 這一系列文章旨在幫助大家理解 Collector 的執(zhí)行流程,至于實(shí)現(xiàn)的是否高效、是否優(yōu)雅、是否...
    hylexus閱讀 638評(píng)論 0 1
  • [TOC] 聲明 這一系列文章旨在幫助大家理解 Collector 的執(zhí)行流程,至于實(shí)現(xiàn)的是否高效、是否優(yōu)雅、是否...
    hylexus閱讀 701評(píng)論 0 1
  • [TOC] 聲明 這一系列文章旨在幫助大家理解 Collector 的執(zhí)行流程,至于實(shí)現(xiàn)的是否高效、是否優(yōu)雅、是否...
    hylexus閱讀 2,269評(píng)論 0 0
  • 原文地址 http://blog.csdn.net/myherux/article/details/7185511...
    Vissioon閱讀 823評(píng)論 0 0

友情鏈接更多精彩內(nèi)容