【问题标题】:Count int occurrences with Java8使用 Java8 计算 int 的出现次数
【发布时间】:2014-05-29 03:36:04
【问题描述】:

有没有更好的方法来用 Java8 计算 int 的出现次数

int[] monthCounter = new int[12];
persons.stream().forEach(person -> monthCounter[person.getBirthday().getMonthValue() - 1]++);

【问题讨论】:

    标签: java java-8


    【解决方案1】:

    试试:

     Map<Integer, Long> counters = persons.stream()
         .collect(Collectors.groupingBy(p -> p.getBirthday().getMonthValue(), 
             Collectors.counting()));
    

    【讨论】:

    • 这是一个非常好的主意,但是有没有办法将最终计数放入数组而不是 Map?
    • Arrays.setAll(array, counters::get);
    • 这是什么?计数器::get
    • 是绑定方法引用,相当于lambda i -&gt; counters.get(i)
    • @JohnnyWiller "Bound" 表示方法调用的接收者在 lambda 捕获时被绑定。
    【解决方案2】:

    这可能需要一些变化。

    您可以使用Collectors.summingInt() 来使用Integer,而不是计数中的Long

    如果您想跳过原始 int 数组,可以在一次迭代中将计数直接存储到 List

    以整数计算出生月份

    Map<Integer, Integer> monthsToCounts = 
            people.stream().collect(
                    Collectors.groupingBy(p -> p.getBirthday().getMonthValue(), 
                    Collectors.summingInt(a -> 1)));
    

    将出生月份存储在从 0 开始的数组中

    int[] monthCounter = new int[12];
    people.stream().collect(Collectors.groupingBy(p -> p.getBirthday().getMonthValue(), 
                            Collectors.summingInt(a -> 1)))
                            .forEach((month, count) -> monthCounter[month-1]=count);
    

    跳过数组并将值直接存储到列表中

    List<Integer> counts = people.stream().collect(
            Collectors.groupingBy(p -> p.getBirthday().getMonthValue(), 
            Collectors.summingInt(a -> 1)))
            .values().stream().collect(Collectors.toList());
    

    【讨论】:

      【解决方案3】:

      通过Eclipse Collections(以前的GS Collections),您可以使用称为Bag 的数据结构,它可以保存每个元素的出现次数。

      使用IntBag,以下将起作用:

      MutableList<Person> personsEC = ListAdapter.adapt(persons);
      IntBag intBag = personsEC.collectInt(person -> person.getBirthDay().getMonthValue()).toBag();
      
      intBag.forEachWithOccurrences((month, count) -> System.out.println("Count of month:" + month + " is " + count));
      

      如果您想使用数组来跟踪计数,可以结合Arrays.setAll() 方法Brian 在另一个答案中指出。

      int[] monthCounter  = new int[12];
      MutableList<Person> personsEC = ListAdapter.adapt(persons);
      IntBag bag = personsEC.collectInt(person -> person.getBirthDay().getMonthValue()).toBag();
      Arrays.setAll(monthCounter, bag::occurrencesOf);
      System.out.println(IntLists.immutable.with(monthCounter));
      

      如果您使用匿名内部类而不是 lambda,此代码也适用于 Java 5 – 7。

      注意:我是 Eclipse Collections 的提交者

      【讨论】:

        【解决方案4】:

        如果您想获得 Integer 到 Integer 的映射,您可以执行以下操作。

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

        【讨论】:

          【解决方案5】:

          已经回答了。我的小建议,以消除 null 指针异常 即从流中null会抛出java.lang.UnsupportedOperationException, java.lang.NullPointerException

          Map<Integer, Long> birthdayCount = persons.stream()
                                              .filter(Objects::nonNull) // filter out null object
                                              .filter(p->Objects.nonNull(p.getBirthday())) // filter out null birthdays
                                              .collect(Collectors.groupingBy(p -> 
                                                           p.getBirthday().getMonthValue(), 
                                                           Collectors.counting()));
          

          【讨论】:

            【解决方案6】:
            int size = persons.stream().count()
            

            【讨论】:

            • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
            • count() 返回 long 你需要转换为 int,所以这不会编译。应该是int size = (int) persons.stream().count()
            猜你喜欢
            • 1970-01-01
            • 2017-02-13
            • 2019-01-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-04-06
            • 1970-01-01
            相关资源
            最近更新 更多