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方法實現

相關文章
相關標籤/搜索