Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: 9
    • Fix Version/s: 9
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b99
    • CPU:
      generic
    • OS:
      generic

      Description


      When I consider the operation what is "grouping the number of employees whose income is over 2000 by the depertment from employees", we have to write following because there is no way to filter for Collector:
      (Note: In this case, we need the entry which the value is 0)

      Map<Department, Long> map = emps.stream()
          .collect(groupingBy(Employee::getDepartment,
              collectingAndThen(toList(),
                  es -> es.stream().filter(e -> e.getSalary() > 2000).count())));

      When I add filtering like following to Collectors, we can write it easy, and it would be more efficient.
          public static <T, A, R>
          Collector<T, ?, R> filtering(Predicate<? super T> filter,
                                     Collector<? super T, A, R> downstream) {
              BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
              return new CollectorImpl<>(downstream.supplier(),
                                         (r, t) -> {
                                             if (filter.test(t)) {
                                                 downstreamAccumulator.accept(r, t);
                                             }
                                         },
                                         downstream.combiner(), downstream.finisher(),
                                         downstream.characteristics());
          }

      Map<Department, Long> map = emps.stream()
          .collect(groupingBy(Employee::getDepartment,
              filtering(e -> e.getSalary() > 2000, counting())));

        Attachments

          Activity

            People

            • Assignee:
              shinyafox Shinya Yoshida
              Reporter:
              shinyafox Shinya Yoshida
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: