jdk8 自定义收集器

自定义收集器

public class MySetCollector<T> implements Collector<T, Set<T>,Set<T>> {
    @Override
    public Supplier<Set<T>> supplier() {
        System.out.println("supplier invoked");
        return HashSet<T>::new;
    }

    @Override
    public BiConsumer<Set<T>, T> accumulator() {
        System.out.println("accumulator invoked");
        //不能使用HashSet<T>::add 与Supplier那一块产生的容器可能有冲突 因此必须使用Set<T>
        //return (set,item) -> set.add(item);
        return Set<T>::add;
    }

    @Override
    public BinaryOperator<Set<T>> combiner() {
        System.out.println("combiner invoked");
        return (set1,set2)->{
          set1.addAll(set2);
          return set1;
        };
    }

    @Override
    public Function<Set<T>, Set<T>> finisher() {
        System.out.println("finisher invoked");
        return Function.identity();
    }

    @Override
    public Set<Characteristics> characteristics() {
        System.out.println("characteristics invoked");
        return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH,UNORDERED));
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello","world","welcome");
        Set<String> set = list.stream().collect( new MySetCollector<>());
        System.out.println(set);
    }
}

复制代码
  • 输入:Set
  • 输出:Map<String,String> 示例输入 :[“hello”,"world","hello world"] 示例输出:[{hello,hello},{world,world},{hello world,hello world}]
public class MySetCollector2<T> implements Collector<T, Set<T>,Map<T,T>> {
    @Override
    public Supplier<Set<T>> supplier() {
        System.out.println("supplier invoked");
        return HashSet<T>::new;
    }

    @Override
    public BiConsumer<Set<T>, T> accumulator() {
        System.out.println("accumulator invoked");
        //不能使用HashSet<T>::add 与Supplier那一块产生的容器可能有冲突 因此必须使用Set<T>
        //return (set,item) -> set.add(item);
        return Set<T>::add;
    }

    @Override
    public BinaryOperator<Set<T>> combiner() {
        System.out.println("combiner invoked");
        return (set1,set2)->{
          set1.addAll(set2);
          return set1;
        };
    }

    @Override
    public Function<Set<T>,Map<T,T>> finisher() {
        System.out.println("finisher invoked");
        return set ->{
            Map<T,T> map = new HashMap<>();
            set.stream.forEach(item -> map.put(item,item));
            return map;
        };
    }

    @Override
    public Set<Characteristics> characteristics() {
        System.out.println("characteristics invoked");
        return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH,UNORDERED));
    }

    public static void main(String[] args) {
        List<String> list = Arrays.asList("hello","world","welcome");
        Set<String> set = list.stream().collect( new MySetCollector<>());
        System.out.println(set);
    }
}

复制代码
  • 主函数
List<String> list = Arrays.asList("hello","world","welcome","hello","a","b","c","d","e","f","g")

Set<String> set = new HashSet<>();
set.addAll(list);
System.out.println("set:"+set);
Map<String,String> map = set.stream().collect(new MySetCollector2<>());
System.out.println(map);
复制代码

IDENTITY_FINISH:将不使用finish函数 而后把Set<T,T>强制转换为Map<T,T>,结果会出错bash

CONCURENT:多个线程同时操做一个结果容器,不须要使用combiner 若是不加这个属性,parallelStream能够多个线程操做多个中间容器 最终由combiner函数合并多个中间容器ide

jdk方法实现

相关文章
相关标签/搜索