Counting elements of a Stream

后端 未结 3 677
攒了一身酷
攒了一身酷 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:34

    The following should work if you are not using parallel streams:

    final int[] counter = {0};
    stream.peek(s -> counter[0]++);
    

提交回复
热议问题