Java lambda表达式如何自定义一个toList Collector

匿名类:

java 复制代码
package l8;

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

public class CollectorToList2 {
    public static void main(String[] args) {

        /**
         *
         <T> -- the type of input elements to the reduction operation
         <A> -- the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
         <R> -- the result type of the reduction operation  最终返回结果的类型
         */
        Collector<Integer, List<Integer>, List<String>> toList = new Collector<Integer, List<Integer>, List<String>>() {

            // 初始一个容器,用于做为累加的容器
            @Override
            public Supplier<List<Integer>> supplier() {
                return () -> new ArrayList<>();
            }

            /**
             * 元素累加
             * @return
             */
            @Override
            public BiConsumer<List<Integer>, Integer> accumulator() {
                return List::add;
            }

            /**
             * 将多个容器进行合并(应该是在并行Stream时使用的)
             * @return
             */
            @Override
            public BinaryOperator<List<Integer>> combiner() {
                return (a, b) -> {
                    System.out.println("combiner call");
                    a.addAll(b);
                    return a;
                };
            }

            /**
             * 最终类型转换
             * @return
             */
            @Override
            public Function<List<Integer>, List<String>> finisher() {
                return list -> list.stream()
                        .map(e -> e + "").collect(Collectors.toList());
            }


            @Override
            public Set<Characteristics> characteristics() {
                return Collections.singleton(Characteristics.UNORDERED);
            }
        };


        List<String> collect = Arrays.asList(1, 2, 3).stream().collect(toList);
        System.out.println(collect);

        collect = Arrays.asList(1, 2, 3).parallelStream().collect(toList);
        System.out.println(collect);
    }
}
javascript:void(0)

Combiner:

应用:

优化初始容器的容量:

java 复制代码
/**
 * <T> -- the type of input elements to the reduction operation
 * <A> -- the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
 * <R> -- the result type of the reduction operation  最终返回结果的类型
 */
class ToListWithInitialCapacity implements Collector<Integer, List<Integer>, List<String>> {
    private int initialCapacity;

    public ToListWithInitialCapacity(int initialCapacity) {
        this.initialCapacity = initialCapacity;
    }

    // 初始一个容器,用于做为累加的容器
    @Override
    public Supplier<List<Integer>> supplier() {
        return () -> new ArrayList<>(initialCapacity);
    }

    /**
     * 元素累加
     *
     * @return
     */
    @Override
    public BiConsumer<List<Integer>, Integer> accumulator() {
        return List::add;
    }

    /**
     * 将多个容器进行合并(应该是在并行Stream时使用的)
     *
     * @return
     */
    @Override
    public BinaryOperator<List<Integer>> combiner() {
        return (a, b) -> {
            System.out.println("combiner call");
            a.addAll(b);
            return a;
        };
    }

    /**
     * 最终类型转换
     *
     * @return
     */
    @Override
    public Function<List<Integer>, List<String>> finisher() {
        return list -> list.stream()
                .map(e -> e + "").collect(Collectors.toList());
    }


    @Override
    public Set<Characteristics> characteristics() {
        return Collections.singleton(Characteristics.UNORDERED);
    }
}

Jdk toList默认实现:

java 复制代码
    /**
     * Returns a {@code Collector} that accumulates the input elements into a
     * new {@code List}. There are no guarantees on the type, mutability,
     * serializability, or thread-safety of the {@code List} returned; if more
     * control over the returned {@code List} is required, use {@link #toCollection(Supplier)}.
     *
     * @param <T> the type of the input elements
     * @return a {@code Collector} which collects all the input elements into a
     * {@code List}, in encounter order
     */
    public static <T>
    Collector<T, ?, List<T>> toList() {
        return new CollectorImpl<>(ArrayList::new, List::add,
                                   (left, right) -> { left.addAll(right); return left; },
                                   CH_ID);
    }
相关推荐
天天摸鱼的java工程师3 分钟前
假设你在开发订单系统时遇到高并发下库存扣减出错,如何解决?由浅入深分析
java·后端·面试
都叫我大帅哥4 分钟前
Redis的ZSet:从“青铜”到“王者”的排序神器
java·redis
Mr_Xuhhh13 分钟前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
肖笙XiaoSheng15 分钟前
使用Gemini2.5 pro 优化我的定时任务(二)
java·后端·代码规范
小小霸王龙!16 分钟前
互联网大厂Java面试实录:Spring Boot与微服务在电商场景中的应用
java·spring boot·redis·微服务·电商
旺旺大力包17 分钟前
【JS笔记】JS 和 noodjs 的常见操作(十)
开发语言·javascript·node.js·ecmascript
深栈解码19 分钟前
JUC并发编程 CAS运行机制详解
java·后端
草履虫建模20 分钟前
Postman - API 调试与开发工具 - 标准使用流程
java·测试工具·spring·json·测试用例·postman·集成学习
深栈解码20 分钟前
JUC并发编程 ThreadLocal解析
java·后端
衍生星球27 分钟前
Maven 3.9.6的下载和配置
java·maven·springboot