DengQN·一个普通程序员;
定制收集器Collector-实现GroupingBy
2018-09-30 17:00 67
#类型#实现#收集器#用于#返回#确定#元素#最终

确定类型

定制collector要实现Collector接口,首先要确定类型

  • 待收集元素的类型
  • 累加器/accumulate 的类型
  • 最终结果的类型

假设要实现这么个收集器:

public class GroupingBy<T,K> implements Collector<T,Map<K,List<T>>,Map<K,List<T>>>

类型分别为:

  • T
  • Map<K,List<T>>
  • Map<K,List<T>>

实现收集器的组件

收集器有4个重要的组件,他们都是函数

  • supplier
  • accumulator
  • combiner
  • finisher

supplier

supplier 用于创建容器.

@Override
    public Supplier<Map<K, List<T>>> supplier() {
        return ()-> new HashMap<>();
    }

accumulator是叠加器,相当于reduce里面的第二个参数,用于将下一个内容加入到前面的结果.

 @Override
    public BiConsumer<Map<K, List<T>>, T> accumulator() {
        return (accumulator,ele)->{
            K key = this.classifier.apply(ele);
            List<T> tList = accumulator.get(key);
            if (tList == null){
                tList = new ArrayList<>();
            }
            tList.add(ele);
            accumulator.put(key,tList);
        };
    }

在添加下一个元素之前判断map中有无list

关键的一点是key的获取.由传进来的一个classifier完成,通过classifier 获得key.

combiner

相当于reduce的参数3,用于将产生的各个容器合并起来

@Override
    public BinaryOperator<Map<K, List<T>>> combiner() {
        return (l,r)->{
            l.putAll(r);
            return l;
        };
    }

直接把后一个装到前一个并返回就行

finisher

描述返回最终的结果.

@Override
    public Function<Map<K, List<T>>, Map<K, List<T>>> finisher() {
        return accumulator->accumulator;
    }

额外 characteristics

描述数据的返回形式

@Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH));
    }

相关解释:

    /**
     * Characteristics indicating properties of a {@code Collector}, which can
     * be used to optimize reduction implementations.
     */
    enum Characteristics {
        /**
         * Indicates that this collector is <em>concurrent</em>, meaning that
         * the result container can support the accumulator function being
         * called concurrently with the same result container from multiple
         * threads.
         *
         * <p>If a {@code CONCURRENT} collector is not also {@code UNORDERED},
         * then it should only be evaluated concurrently if applied to an
         * unordered data source.
         */
        CONCURRENT,

        /**
         * Indicates that the collection operation does not commit to preserving
         * the encounter order of input elements.  (This might be true if the
         * result container has no intrinsic order, such as a {@link Set}.)
         */
        UNORDERED,

        /**
         * Indicates that the finisher function is the identity function and
         * can be elided.  If set, it must be the case that an unchecked cast
         * from A to R will succeed.
         */
        IDENTITY_FINISH
    }

test

image