Count int occurrences with Java8

后端 未结 6 920
北恋
北恋 2020-11-27 06:12

Is there a better way to count int occurrences with Java8

int[] monthCounter = new int[12];
persons.stream().forEach(person -> monthCounter[person.getBirt         


        
6条回答
  •  臣服心动
    2020-11-27 06:33

    If you would like to get Integer to Integer map, you can do the following.

    Map counters = persons.stream()
        .collect(Collectors.groupingBy(
            p -> p.getBirthday().getMonthValue(),
            Collectors.reducing(0, e -> 1, Integer::sum)));
    

提交回复
热议问题