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,38 **** 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; --- 28,37 ----
*** 76,97 **** * .collect(Collectors.toStringJoiner(", ")) * .toString(); * * // Find highest-paid employee * Employee highestPaid = employees.stream() ! * .collect(Collectors.maxBy(Comparators.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)))); * * // Partition students into passing and failing * Map<Boolean, List<Student>> passingFailing = * students.stream() * .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD); --- 75,96 ---- * .collect(Collectors.toStringJoiner(", ")) * .toString(); * * // Find highest-paid employee * Employee highestPaid = employees.stream() ! * .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(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,440 **** * according to a given {@code Comparator}. * * @implSpec * This produces a result equivalent to: * <pre>{@code ! * reducing(Comparators.lesserOf(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)); } /** * 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)) * }</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)); } /** * Returns a {@code Collector<T, Long>} that produces the sum of a * long-valued function applied to the input element. --- 401,439 ---- * according to a given {@code Comparator}. * * @implSpec * This produces a result equivalent to: * <pre>{@code ! * 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(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(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(BinaryOperator.maxBy(comparator)); } /** * Returns a {@code Collector<T, Long>} that produces the sum of a * long-valued function applied to the input element.
*** 489,500 **** * 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); * Map<City, Person> tallestByCity * = people.stream().collect(groupingBy(Person::getCity, reducing(tallerOf))); * }</pre> * * @implSpec --- 488,499 ---- * 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 = 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,540 **** * 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); * Map<City, String> longestLastNameByCity * = people.stream().collect(groupingBy(Person::getCity, * reducing(Person::getLastName, longerOf))); * }</pre> * --- 528,539 ---- * 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 = 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> *