JAVA8-自定义Collector

admin
2022-09-19 / 0 评论 / 201 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年09月19日,已超过795天没有更新,若内容或图片失效,请留言反馈。

自定义Collector

实现Collector接口

package com.example.study.java8.collector;

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;

/**
 * 自定义Collector
 * T:元素类型
 * List<T>:要创建的类型
 * List<T>:最后要返回的类型
 */
public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {
    private void log(final String log) {
        System.out.println(Thread.currentThread().getName()+"-"+log);
    }

    //一定时可变的supplier,不是固定值
    @Override
    public Supplier<List<T>> supplier() {
        log("supplier");
        return ArrayList::new;
    }

    //要进行的操作
    @Override
    public BiConsumer<List<T>, T> accumulator() {
        log("accumulator");
        return List::add;
    }

    //将结果整合
    @Override
    public BinaryOperator<List<T>> combiner() {
        log("combiner");
        return (list1,list2)->{
            list1.addAll(list2);
            return list1;
        };
    }

    //返回结果
    @Override
    public Function<List<T>, List<T>> finisher() {
        log("finisher");
//        return t->t;
        return Function.identity();
    }

    //特征值:CONCURRENT-并行,UNORDERED-排序,IDENTITY_FINISH-入参就出参
    @Override
    public Set<Characteristics> characteristics() {
        log("characteristics");
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.CONCURRENT));
    }
}

使用测试

package com.example.study.java8.collector;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;

/**
 * 使用自定义Collector测试
 */
public class CustomToListCollector {
    public static void main(String[] args) {
        String[] arrs = new String[]{"chinese","japanse","english","freach","Korean"};
        //测试
        //对应接口中的:
        //Supplier<A> supplier(), BiConsumer<A, T> accumulator(),Function<A, R> finisher();
        Collector<String, List<String>, List<String>> collector = new ToListCollector<String>();
        List<String> list = Arrays.stream(arrs).filter(s -> s.length() > 6).collect(collector);
        System.out.println(list);

        System.out.println("================================");

        //parallelStream(),并行使用测试
        List<String> parallelList = Arrays.asList(new String[]{"chinese", "japanse", "english", "freach", "Korean"})
                .parallelStream()
                .filter(s -> s.length() > 6)
                .collect(collector);
        System.out.println(parallelList);
    }
}

输出结果

main-supplier
main-accumulator
main-combiner
main-characteristics
main-characteristics
[chinese, japanse, english]
================================
main-characteristics
main-characteristics
main-supplier
main-accumulator
main-combiner
main-characteristics
main-characteristics
[chinese, japanse, english]

由于数据量少,并没有出现并行。

4

评论 (0)

取消