src/share/classes/java/util/stream/Collectors.java

Print this page
rev 7485 : 8009736: Comparator API cleanup
Reviewed-by:
Contributed-by: henry.jen@oracle.com

@@ -28,11 +28,10 @@
 import java.util.AbstractSet;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.Comparators;
 import java.util.DoubleSummaryStatistics;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.IntSummaryStatistics;

@@ -76,22 +75,22 @@
  *                           .collect(Collectors.toStringJoiner(", "))
  *                           .toString();
  *
  *     // Find highest-paid employee
  *     Employee highestPaid = employees.stream()
- *                                     .collect(Collectors.maxBy(Comparators.comparing(Employee::getSalary)));
+ *                                     .collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary)));
  *
  *     // Group employees by department
  *     Map<Department, List<Employee>> byDept
  *         = employees.stream()
  *                    .collect(Collectors.groupingBy(Employee::getDepartment));
  *
  *     // Find highest-paid employee by department
  *     Map<Department, Employee> highestPaidByDept
  *         = employees.stream()
  *                    .collect(Collectors.groupingBy(Employee::getDepartment,
- *                                                   Collectors.maxBy(Comparators.comparing(Employee::getSalary))));
+ *                                                   Collectors.maxBy(Comparator.comparing(Employee::getSalary))));
  *
  *     // Partition students into passing and failing
  *     Map<Boolean, List<Student>> passingFailing =
  *         students.stream()
  *                 .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD);

@@ -402,39 +401,39 @@
      * according to a given {@code Comparator}.
      *
      * @implSpec
      * This produces a result equivalent to:
      * <pre>{@code
-     *     reducing(Comparators.lesserOf(comparator))
+     *     reducing(BinaryOperator.minBy(comparator))
      * }</pre>
      *
      * @param <T> the type of the input elements
      * @param comparator a {@code Comparator} for comparing elements
      * @return a {@code Collector} that produces the minimal value
      */
     public static <T> Collector<T, T>
     minBy(Comparator<? super T> comparator) {
-        return reducing(Comparators.lesserOf(comparator));
+        return reducing(BinaryOperator.minBy(comparator));
     }
 
     /**
      * Returns a {@code Collector<T, T>} that produces the maximal element
      * according to a given {@code Comparator}.
      *
      * @implSpec
      * This produces a result equivalent to:
      * <pre>{@code
-     *     reducing(Comparators.greaterOf(comparator))
+     *     reducing(BinaryOperator.maxBy(comparator))
      * }</pre>
      *
      * @param <T> the type of the input elements
      * @param comparator a {@code Comparator} for comparing elements
      * @return a {@code Collector} that produces the maximal value
      */
     public static <T> Collector<T, T>
     maxBy(Comparator<? super T> comparator) {
-        return reducing(Comparators.greaterOf(comparator));
+        return reducing(BinaryOperator.maxBy(comparator));
     }
 
     /**
      * Returns a {@code Collector<T, Long>} that produces the sum of a
      * long-valued function applied to the input element.

@@ -489,12 +488,12 @@
      * use {@link Stream#reduce(BinaryOperator)} instead.
      *
      * <p>For example, given a stream of {@code Person}, to calculate tallest
      * person in each city:
      * <pre>{@code
-     *     Comparator<Person> byHeight = Comparators.comparing(Person::getHeight);
-     *     BinaryOperator<Person> tallerOf = Comparators.greaterOf(byHeight);
+     *     Comparator<Person> byHeight = Comparator.comparing(Person::getHeight);
+     *     BinaryOperator<Person> tallerOf = BinaryOperator.greaterOf(byHeight);
      *     Map<City, Person> tallestByCity
      *         = people.stream().collect(groupingBy(Person::getCity, reducing(tallerOf)));
      * }</pre>
      *
      * @implSpec

@@ -529,12 +528,12 @@
      * use {@link Stream#reduce(BinaryOperator)} instead.
      *
      * <p>For example, given a stream of {@code Person}, to calculate the longest
      * last name of residents in each city:
      * <pre>{@code
-     *     Comparator<String> byLength = Comparators.comparing(String::length);
-     *     BinaryOperator<String> longerOf = Comparators.greaterOf(byLength);
+     *     Comparator<String> byLength = Comparator.comparing(String::length);
+     *     BinaryOperator<String> longerOf = BinaryOperator.greaterOf(byLength);
      *     Map<City, String> longestLastNameByCity
      *         = people.stream().collect(groupingBy(Person::getCity,
      *                                              reducing(Person::getLastName, longerOf)));
      * }</pre>
      *