Counting elements of a Stream

后端 未结 3 675
攒了一身酷
攒了一身酷 2020-12-10 03:57

I want to count the different elements of a stream and am wondering why

Stream stream = Stream.of(\"a\", \"b\", \"a\", \"c\", \"c\", \"a\", \"a         


        
3条回答
  •  温柔的废话
    2020-12-10 04:38

        private Collector customIntegerCountingCollector() {
        return Collector.of(
            () -> new int[1],
            (result, ) -> result[0] += 1,
            (result1, result2) -> {
                result1[0] += result2[0];
                return result1;
            },
            total -> Integer.valueOf(total[0])
        );
    }
    

    Inspired by : https://www.deadcoderising.com/2017-03-07-java-8-creating-a-custom-collector-for-your-stream/

提交回复
热议问题