自定义Java 8收集器

时间:2018-10-24 18:39:55

标签: java lambda java-8 collectors

我想检查一下如何实现自定义收集器。

说,我需要做些

(1)对字母频率图和 (2)能够将2个结果合并为一个结果。

class CharHistogram implements Collector<String, Map<Character, Integer>, Map<Character, Integer>> {



    public static CharHistogram toCharHistogram(){
        return new CharHistogram();
    }

    @Override
    public Supplier<Map<Character, Integer>> supplier() {
        SysOut.print("supplier invoked");
        return HashMap::new;
    }

    @Override
    public BiConsumer<Map<Character, Integer>, String> accumulator() {
        SysOut.print("accumulator invoked");
        return (map, val) -> {
            SysOut.print(val +" processed");
            char[] characters = val.toCharArray();
            for (char character : characters) {
                int count = 1;
                if (map.containsKey(character)) {
                    count = map.get(character);
                    count++;
                }
                map.put(character, count);
            }
        };
    }

    @Override
    public BinaryOperator<Map<Character, Integer>> combiner() {
        SysOut.print("combiner invoked");
        return (map1, map2) -> {
            SysOut.print(map1+" merged to "+map2);
            map2.forEach((k, v) -> map1.merge(k, v, (v1, v2) -> v1 + v2));
            return map1;
        };
    }

    @Override
    public Function<Map<Character, Integer>, Map<Character, Integer>> finisher() {
        SysOut.print("finisher invoked");
        return Function.identity();
    }

    @Override
    public Set<java.util.stream.Collector.Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(Characteristics.IDENTITY_FINISH, Characteristics.UNORDERED));
    }

}

客户代码:

CharHistogram charStatsState = CharHistogram.toCharHistogram();

Map<Character, Integer> charCountMap = Arrays.asList("apple","orange","orange").stream().collect(charStatsState);
SysOut.print(charCountMap);
charCountMap = Arrays.asList("pears","pears","orange").stream().collect(charStatsState);
SysOut.print(charCountMap);

输出:

[main]: supplier invoked
[main]: accumulator invoked
[main]: combiner invoked
[main]: apple processed
[main]: orange processed
[main]: orange processed
[main]: {p=2, a=3, r=2, e=3, g=2, l=1, n=2, o=2}
[main]: supplier invoked
[main]: accumulator invoked
[main]: combiner invoked
[main]: pears processed
[main]: pears processed
[main]: orange processed
[main]: {p=2, a=3, r=3, s=2, e=3, g=1, n=1, o=1}

我看不到合并器或整理器都被调用,我认为这些必须正确设计才能实现我想要的功能。

我想念什么?

编辑:

支持流和组合器的一种可能方法。以下代码无法正常工作。

class CharStreamHistogram implements Function<String, Map<Character, Integer>>{

    private int totalCharactersRead;
    private Map<Character, Integer> histogram;

    public int getTotalCharactersRead() {
        return totalCharactersRead;
    }
    public Map<Character, Integer> getHistogram() {
        return histogram;
    }
    public void setHistogram(Map<Character, Integer> histogram) {
        this.histogram = histogram;
    }
    public void setTotalCharactersRead(int totalCharactersRead) {
        this.totalCharactersRead = totalCharactersRead;
    }

    public Map<Character, Integer> combine(Map<Character, Integer>  map2) {
        Map<Character, Integer> map1 = this.histogram;
        map2.forEach((k, v) -> map1.merge(k, v, (v1, v2) -> v1 + v2));
        return map2;
    }


    @Override
    public Map<Character, Integer> apply(String val) {
        char[] characters = val.toCharArray();
        totalCharactersRead += characters.length;
        for (char character : characters) {
            int count = 1;
            if (histogram.containsKey(character)) {
                count = histogram.get(character);
                count++;
            }
            histogram.put(character, count);
        }
        return histogram;
    }

} 

public static <T> Collector<T, ?, CharStreamHistogram> summarizeCharStream(
             CharStreamHistogram histogram) { //TODO: is this correct?
        Collector charStatsState = new Collector<String, CharStreamHistogram, CharStreamHistogram>() {

            @Override
            public Supplier<CharStreamHistogram> supplier() {
                return CharStreamHistogram::new;
            }

            @Override
            public BiConsumer<CharStreamHistogram, String> accumulator() {
                //TODO: What to do here?
                return null;
            }

            @Override
            public BinaryOperator<CharStreamHistogram> combiner() {
                BinaryOperator binaryOperator = (l, r) -> {
                    l.combine(r); //TODO: Something like this?
                };
                return binaryOperator;
            }

            @Override
            public Function<CharStreamHistogram, CharStreamHistogram> finisher() {
                //TODO: What to do here?
                return null;
            }

            @Override
            public Set<java.util.stream.Collector.Characteristics> characteristics() {
                return Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED));
            }
        };
        return charStatsState;
    }

1 个答案:

答案 0 :(得分:6)

好吧,您已经声明了Characteristics.IDENTITY_FINISH-明确表示不会调用finisher,并且只有在并行流的情况下才调用combiner