Java 8 stream combiner never called

前端 未结 2 1787
误落风尘
误落风尘 2021-02-05 04:25

I\'m writing a custom java 8 collector which is supposed to compute the average of a POJO which has a getValue() method. Here\'s the code:

public st         


        
2条回答
  •  别跟我提以往
    2021-02-05 05:13

    Well, that’s exactly what you request when specifying Characteristics.CONCURRENT:

    Indicates that this collector is concurrent, meaning that the result container can support the accumulator function being called concurrently with the same result container from multiple threads.

    If that’s not the case, as with your Collector, you shouldn’t specify that flag.


    As a side note, new HashSet(Arrays.asList(Characteristics.CONCURRENT, Characteristics.UNORDERED)); is quite inefficient for specifying characteristics. You can just use EnumSet.of(Characteristics.CONCURRENT, Characteristics.UNORDERED). When you remove the wrong concurrent characteristic, you may use either EnumSet.of(Characteristics.UNORDERED) or Collections.singleton(Characteristics.UNORDERED), but a HashSet definitely is overkill.

提交回复
热议问题