1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package java.util.stream; 26 27 import java.util.AbstractMap; 28 import java.util.AbstractSet; 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.Collection; 32 import java.util.Collections; 33 import java.util.Comparator; 34 import java.util.DoubleSummaryStatistics; 35 import java.util.EnumSet; 36 import java.util.HashMap; 37 import java.util.HashSet; 38 import java.util.IntSummaryStatistics; 39 import java.util.Iterator; 40 import java.util.List; 41 import java.util.LongSummaryStatistics; 42 import java.util.Map; 43 import java.util.Objects; 44 import java.util.Optional; 45 import java.util.Set; 46 import java.util.StringJoiner; 47 import java.util.concurrent.ConcurrentHashMap; 48 import java.util.concurrent.ConcurrentMap; 49 import java.util.function.BiConsumer; 50 import java.util.function.BiFunction; 51 import java.util.function.BinaryOperator; 52 import java.util.function.Consumer; 53 import java.util.function.Function; 54 import java.util.function.Predicate; 55 import java.util.function.Supplier; 56 import java.util.function.ToDoubleFunction; 57 import java.util.function.ToIntFunction; 58 import java.util.function.ToLongFunction; 59 60 /** 61 * Implementations of {@link Collector} that implement various useful reduction 62 * operations, such as accumulating elements into collections, summarizing 63 * elements according to various criteria, etc. 64 * 65 * <p>The following are examples of using the predefined collectors to perform 66 * common mutable reduction tasks: 67 * 68 * <pre>{@code 69 * // Accumulate names into a List 70 * List<String> list = people.stream().map(Person::getName).collect(Collectors.toList()); 71 * 72 * // Accumulate names into a TreeSet 73 * Set<String> set = people.stream().map(Person::getName).collect(Collectors.toCollection(TreeSet::new)); 74 * 75 * // Convert elements to strings and concatenate them, separated by commas 76 * String joined = things.stream() 77 * .map(Object::toString) 78 * .collect(Collectors.joining(", ")); 79 * 80 * // Compute sum of salaries of employee 81 * int total = employees.stream() 82 * .collect(Collectors.summingInt(Employee::getSalary))); 83 * 84 * // Group employees by department 85 * Map<Department, List<Employee>> byDept 86 * = employees.stream() 87 * .collect(Collectors.groupingBy(Employee::getDepartment)); 88 * 89 * // Compute sum of salaries by department 90 * Map<Department, Integer> totalByDept 91 * = employees.stream() 92 * .collect(Collectors.groupingBy(Employee::getDepartment, 93 * Collectors.summingInt(Employee::getSalary))); 94 * 95 * // Partition students into passing and failing 96 * Map<Boolean, List<Student>> passingFailing = 97 * students.stream() 98 * .collect(Collectors.partitioningBy(s -> s.getGrade() >= PASS_THRESHOLD)); 99 * 100 * }</pre> 101 * 102 * @since 1.8 103 */ 104 public final class Collectors { 105 106 static final Set<Collector.Characteristics> CH_CONCURRENT_ID 107 = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT, 108 Collector.Characteristics.UNORDERED, 109 Collector.Characteristics.IDENTITY_FINISH)); 110 static final Set<Collector.Characteristics> CH_CONCURRENT_NOID 111 = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.CONCURRENT, 112 Collector.Characteristics.UNORDERED)); 113 static final Set<Collector.Characteristics> CH_ID 114 = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.IDENTITY_FINISH)); 115 static final Set<Collector.Characteristics> CH_UNORDERED_ID 116 = Collections.unmodifiableSet(EnumSet.of(Collector.Characteristics.UNORDERED, 117 Collector.Characteristics.IDENTITY_FINISH)); 118 static final Set<Collector.Characteristics> CH_NOID = Collections.emptySet(); 119 120 private Collectors() { } 121 122 /** 123 * Construct an {@code IllegalStateException} with appropriate message. 124 * 125 * @param k the duplicate key 126 * @param u 1st value to be accumulated/merged 127 * @param v 2nd value to be accumulated/merged 128 */ 129 private static IllegalStateException duplicateKeyException( 130 Object k, Object u, Object v) { 131 return new IllegalStateException(String.format( 132 "Duplicate key %s (attempted merging values %s and %s)", 133 k, u, v)); 134 } 135 136 /** 137 * {@code BinaryOperator<Map>} that merges the contents of its right 138 * argument into its left argument, throwing {@code IllegalStateException} 139 * if duplicate keys are encountered. 140 * 141 * @param <K> type of the map keys 142 * @param <V> type of the map values 143 * @param <M> type of the map 144 * @return a merge function for two maps 145 */ 146 private static <K, V, M extends Map<K,V>> 147 BinaryOperator<M> uniqKeysMapMerger() { 148 return (m1, m2) -> { 149 for (Map.Entry<K,V> e : m2.entrySet()) { 150 K k = e.getKey(); 151 V v = Objects.requireNonNull(e.getValue()); 152 V u = m1.putIfAbsent(k, v); 153 if (u != null) throw duplicateKeyException(k, u, v); 154 } 155 return m1; 156 }; 157 } 158 159 /** 160 * {@code BiConsumer<Map, T>} that accumulates (key, value) pairs 161 * extracted from elements into the map, throwing {@code IllegalStateException} 162 * if duplicate keys are encountered. 163 * 164 * @param keyMapper a function that maps an element into a key 165 * @param valueMapper a function that maps an element into a value 166 * @param <T> type of elements 167 * @param <K> type of map keys 168 * @param <V> type of map values 169 * @return an accumulating consumer 170 */ 171 private static <T, K, V> 172 BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, 173 Function<? super T, ? extends V> valueMapper) { 174 return (map, element) -> { 175 K k = keyMapper.apply(element); 176 V v = Objects.requireNonNull(valueMapper.apply(element)); 177 V u = map.putIfAbsent(k, v); 178 if (u != null) throw duplicateKeyException(k, u, v); 179 }; 180 } 181 182 @SuppressWarnings("unchecked") 183 private static <I, R> Function<I, R> castingIdentity() { 184 return i -> (R) i; 185 } 186 187 /** 188 * Simple implementation class for {@code Collector}. 189 * 190 * @param <T> the type of elements to be collected 191 * @param <R> the type of the result 192 */ 193 static class CollectorImpl<T, A, R> implements Collector<T, A, R> { 194 private final Supplier<A> supplier; 195 private final BiConsumer<A, T> accumulator; 196 private final BinaryOperator<A> combiner; 197 private final Function<A, R> finisher; 198 private final Set<Characteristics> characteristics; 199 200 CollectorImpl(Supplier<A> supplier, 201 BiConsumer<A, T> accumulator, 202 BinaryOperator<A> combiner, 203 Function<A,R> finisher, 204 Set<Characteristics> characteristics) { 205 this.supplier = supplier; 206 this.accumulator = accumulator; 207 this.combiner = combiner; 208 this.finisher = finisher; 209 this.characteristics = characteristics; 210 } 211 212 CollectorImpl(Supplier<A> supplier, 213 BiConsumer<A, T> accumulator, 214 BinaryOperator<A> combiner, 215 Set<Characteristics> characteristics) { 216 this(supplier, accumulator, combiner, castingIdentity(), characteristics); 217 } 218 219 @Override 220 public BiConsumer<A, T> accumulator() { 221 return accumulator; 222 } 223 224 @Override 225 public Supplier<A> supplier() { 226 return supplier; 227 } 228 229 @Override 230 public BinaryOperator<A> combiner() { 231 return combiner; 232 } 233 234 @Override 235 public Function<A, R> finisher() { 236 return finisher; 237 } 238 239 @Override 240 public Set<Characteristics> characteristics() { 241 return characteristics; 242 } 243 } 244 245 /** 246 * Returns a {@code Collector} that accumulates the input elements into a 247 * new {@code Collection}, in encounter order. The {@code Collection} is 248 * created by the provided factory. 249 * 250 * @param <T> the type of the input elements 251 * @param <C> the type of the resulting {@code Collection} 252 * @param collectionFactory a {@code Supplier} which returns a new, empty 253 * {@code Collection} of the appropriate type 254 * @return a {@code Collector} which collects all the input elements into a 255 * {@code Collection}, in encounter order 256 */ 257 public static <T, C extends Collection<T>> 258 Collector<T, ?, C> toCollection(Supplier<C> collectionFactory) { 259 return new CollectorImpl<>(collectionFactory, Collection<T>::add, 260 (r1, r2) -> { r1.addAll(r2); return r1; }, 261 CH_ID); 262 } 263 264 /** 265 * Returns a {@code Collector} that accumulates the input elements into a 266 * new {@code List}. There are no guarantees on the type, mutability, 267 * serializability, or thread-safety of the {@code List} returned; if more 268 * control over the returned {@code List} is required, use {@link #toCollection(Supplier)}. 269 * 270 * @param <T> the type of the input elements 271 * @return a {@code Collector} which collects all the input elements into a 272 * {@code List}, in encounter order 273 */ 274 public static <T> 275 Collector<T, ?, List<T>> toList() { 276 return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new, List::add, 277 (left, right) -> { left.addAll(right); return left; }, 278 CH_ID); 279 } 280 281 /** 282 * Returns a {@code Collector} that accumulates the input elements into a 283 * new {@code Set}. There are no guarantees on the type, mutability, 284 * serializability, or thread-safety of the {@code Set} returned; if more 285 * control over the returned {@code Set} is required, use 286 * {@link #toCollection(Supplier)}. 287 * 288 * <p>This is an {@link Collector.Characteristics#UNORDERED unordered} 289 * Collector. 290 * 291 * @param <T> the type of the input elements 292 * @return a {@code Collector} which collects all the input elements into a 293 * {@code Set} 294 */ 295 public static <T> 296 Collector<T, ?, Set<T>> toSet() { 297 return new CollectorImpl<>((Supplier<Set<T>>) HashSet::new, Set::add, 298 (left, right) -> { left.addAll(right); return left; }, 299 CH_UNORDERED_ID); 300 } 301 302 /** 303 * Returns a {@code Collector} that concatenates the input elements into a 304 * {@code String}, in encounter order. 305 * 306 * @return a {@code Collector} that concatenates the input elements into a 307 * {@code String}, in encounter order 308 */ 309 public static Collector<CharSequence, ?, String> joining() { 310 return new CollectorImpl<CharSequence, StringBuilder, String>( 311 StringBuilder::new, StringBuilder::append, 312 (r1, r2) -> { r1.append(r2); return r1; }, 313 StringBuilder::toString, CH_NOID); 314 } 315 316 /** 317 * Returns a {@code Collector} that concatenates the input elements, 318 * separated by the specified delimiter, in encounter order. 319 * 320 * @param delimiter the delimiter to be used between each element 321 * @return A {@code Collector} which concatenates CharSequence elements, 322 * separated by the specified delimiter, in encounter order 323 */ 324 public static Collector<CharSequence, ?, String> joining(CharSequence delimiter) { 325 return joining(delimiter, "", ""); 326 } 327 328 /** 329 * Returns a {@code Collector} that concatenates the input elements, 330 * separated by the specified delimiter, with the specified prefix and 331 * suffix, in encounter order. 332 * 333 * @param delimiter the delimiter to be used between each element 334 * @param prefix the sequence of characters to be used at the beginning 335 * of the joined result 336 * @param suffix the sequence of characters to be used at the end 337 * of the joined result 338 * @return A {@code Collector} which concatenates CharSequence elements, 339 * separated by the specified delimiter, in encounter order 340 */ 341 public static Collector<CharSequence, ?, String> joining(CharSequence delimiter, 342 CharSequence prefix, 343 CharSequence suffix) { 344 return new CollectorImpl<>( 345 () -> new StringJoiner(delimiter, prefix, suffix), 346 StringJoiner::add, StringJoiner::merge, 347 StringJoiner::toString, CH_NOID); 348 } 349 350 /** 351 * {@code BinaryOperator<Map>} that merges the contents of its right 352 * argument into its left argument, using the provided merge function to 353 * handle duplicate keys. 354 * 355 * @param <K> type of the map keys 356 * @param <V> type of the map values 357 * @param <M> type of the map 358 * @param mergeFunction A merge function suitable for 359 * {@link Map#merge(Object, Object, BiFunction) Map.merge()} 360 * @return a merge function for two maps 361 */ 362 private static <K, V, M extends Map<K,V>> 363 BinaryOperator<M> mapMerger(BinaryOperator<V> mergeFunction) { 364 return (m1, m2) -> { 365 for (Map.Entry<K,V> e : m2.entrySet()) 366 m1.merge(e.getKey(), e.getValue(), mergeFunction); 367 return m1; 368 }; 369 } 370 371 /** 372 * Adapts a {@code Collector} accepting elements of type {@code U} to one 373 * accepting elements of type {@code T} by applying a mapping function to 374 * each input element before accumulation. 375 * 376 * @apiNote 377 * The {@code mapping()} collectors are most useful when used in a 378 * multi-level reduction, such as downstream of a {@code groupingBy} or 379 * {@code partitioningBy}. For example, given a stream of 380 * {@code Person}, to accumulate the set of last names in each city: 381 * <pre>{@code 382 * Map<City, Set<String>> lastNamesByCity 383 * = people.stream().collect(groupingBy(Person::getCity, 384 * mapping(Person::getLastName, toSet()))); 385 * }</pre> 386 * 387 * @param <T> the type of the input elements 388 * @param <U> type of elements accepted by downstream collector 389 * @param <A> intermediate accumulation type of the downstream collector 390 * @param <R> result type of collector 391 * @param mapper a function to be applied to the input elements 392 * @param downstream a collector which will accept mapped values 393 * @return a collector which applies the mapping function to the input 394 * elements and provides the mapped results to the downstream collector 395 */ 396 public static <T, U, A, R> 397 Collector<T, ?, R> mapping(Function<? super T, ? extends U> mapper, 398 Collector<? super U, A, R> downstream) { 399 BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); 400 return new CollectorImpl<>(downstream.supplier(), 401 (r, t) -> downstreamAccumulator.accept(r, mapper.apply(t)), 402 downstream.combiner(), downstream.finisher(), 403 downstream.characteristics()); 404 } 405 406 /** 407 * Adapts a {@code Collector} accepting elements of type {@code U} to one 408 * accepting elements of type {@code T} by applying a flat mapping function 409 * to each input element before accumulation. The flat mapping function 410 * maps an input element to a {@link Stream stream} covering zero or more 411 * output elements that are then accumulated downstream. Each mapped stream 412 * is {@link java.util.stream.BaseStream#close() closed} after its contents 413 * have been placed downstream. (If a mapped stream is {@code null} 414 * an empty stream is used, instead.) 415 * 416 * @apiNote 417 * The {@code flatMapping()} collectors are most useful when used in a 418 * multi-level reduction, such as downstream of a {@code groupingBy} or 419 * {@code partitioningBy}. For example, given a stream of 420 * {@code Order}, to accumulate the set of line items for each customer: 421 * <pre>{@code 422 * Map<String, Set<LineItem>> itemsByCustomerName 423 * = orders.stream().collect(groupingBy(Order::getCustomerName, 424 * flatMapping(order -> order.getLineItems().stream(), toSet()))); 425 * }</pre> 426 * 427 * @param <T> the type of the input elements 428 * @param <U> type of elements accepted by downstream collector 429 * @param <A> intermediate accumulation type of the downstream collector 430 * @param <R> result type of collector 431 * @param mapper a function to be applied to the input elements, which 432 * returns a stream of results 433 * @param downstream a collector which will receive the elements of the 434 * stream returned by mapper 435 * @return a collector which applies the mapping function to the input 436 * elements and provides the flat mapped results to the downstream collector 437 * @since 9 438 */ 439 public static <T, U, A, R> 440 Collector<T, ?, R> flatMapping(Function<? super T, ? extends Stream<? extends U>> mapper, 441 Collector<? super U, A, R> downstream) { 442 BiConsumer<A, ? super U> downstreamAccumulator = downstream.accumulator(); 443 return new CollectorImpl<>(downstream.supplier(), 444 (r, t) -> { 445 try (Stream<? extends U> result = mapper.apply(t)) { 446 if (result != null) 447 result.sequential().forEach(u -> downstreamAccumulator.accept(r, u)); 448 } 449 }, 450 downstream.combiner(), downstream.finisher(), 451 downstream.characteristics()); 452 } 453 454 /** 455 * Adapts a {@code Collector} to one accepting elements of the same type 456 * {@code T} by applying the predicate to each input element and only 457 * accumulating if the predicate returns {@code true}. 458 * 459 * @apiNote 460 * The {@code filtering()} collectors are most useful when used in a 461 * multi-level reduction, such as downstream of a {@code groupingBy} or 462 * {@code partitioningBy}. For example, given a stream of 463 * {@code Employee}, to accumulate the employees in each department that have a 464 * salary above a certain threshold: 465 * <pre>{@code 466 * Map<Department, Set<Employee>> wellPaidEmployeesByDepartment 467 * = employees.stream().collect(groupingBy(Employee::getDepartment, 468 * filtering(e -> e.getSalary() > 2000, toSet()))); 469 * }</pre> 470 * A filtering collector differs from a stream's {@code filter()} operation. 471 * In this example, suppose there are no employees whose salary is above the 472 * threshold in some department. Using a filtering collector as shown above 473 * would result in a mapping from that department to an empty {@code Set}. 474 * If a stream {@code filter()} operation were done instead, there would be 475 * no mapping for that department at all. 476 * 477 * @param <T> the type of the input elements 478 * @param <A> intermediate accumulation type of the downstream collector 479 * @param <R> result type of collector 480 * @param predicate a predicate to be applied to the input elements 481 * @param downstream a collector which will accept values that match the 482 * predicate 483 * @return a collector which applies the predicate to the input elements 484 * and provides matching elements to the downstream collector 485 * @since 9 486 */ 487 public static <T, A, R> 488 Collector<T, ?, R> filtering(Predicate<? super T> predicate, 489 Collector<? super T, A, R> downstream) { 490 BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); 491 return new CollectorImpl<>(downstream.supplier(), 492 (r, t) -> { 493 if (predicate.test(t)) { 494 downstreamAccumulator.accept(r, t); 495 } 496 }, 497 downstream.combiner(), downstream.finisher(), 498 downstream.characteristics()); 499 } 500 501 /** 502 * Adapts a {@code Collector} to perform an additional finishing 503 * transformation. For example, one could adapt the {@link #toList()} 504 * collector to always produce an immutable list with: 505 * <pre>{@code 506 * List<String> people 507 * = people.stream().collect(collectingAndThen(toList(), Collections::unmodifiableList)); 508 * }</pre> 509 * 510 * @param <T> the type of the input elements 511 * @param <A> intermediate accumulation type of the downstream collector 512 * @param <R> result type of the downstream collector 513 * @param <RR> result type of the resulting collector 514 * @param downstream a collector 515 * @param finisher a function to be applied to the final result of the downstream collector 516 * @return a collector which performs the action of the downstream collector, 517 * followed by an additional finishing step 518 */ 519 public static<T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, 520 Function<R,RR> finisher) { 521 Set<Collector.Characteristics> characteristics = downstream.characteristics(); 522 if (characteristics.contains(Collector.Characteristics.IDENTITY_FINISH)) { 523 if (characteristics.size() == 1) 524 characteristics = Collectors.CH_NOID; 525 else { 526 characteristics = EnumSet.copyOf(characteristics); 527 characteristics.remove(Collector.Characteristics.IDENTITY_FINISH); 528 characteristics = Collections.unmodifiableSet(characteristics); 529 } 530 } 531 return new CollectorImpl<>(downstream.supplier(), 532 downstream.accumulator(), 533 downstream.combiner(), 534 downstream.finisher().andThen(finisher), 535 characteristics); 536 } 537 538 /** 539 * Returns a {@code Collector} accepting elements of type {@code T} that 540 * counts the number of input elements. If no elements are present, the 541 * result is 0. 542 * 543 * @implSpec 544 * This produces a result equivalent to: 545 * <pre>{@code 546 * reducing(0L, e -> 1L, Long::sum) 547 * }</pre> 548 * 549 * @param <T> the type of the input elements 550 * @return a {@code Collector} that counts the input elements 551 */ 552 public static <T> Collector<T, ?, Long> 553 counting() { 554 return summingLong(e -> 1L); 555 } 556 557 /** 558 * Returns a {@code Collector} that produces the minimal element according 559 * to a given {@code Comparator}, described as an {@code Optional<T>}. 560 * 561 * @implSpec 562 * This produces a result equivalent to: 563 * <pre>{@code 564 * reducing(BinaryOperator.minBy(comparator)) 565 * }</pre> 566 * 567 * @param <T> the type of the input elements 568 * @param comparator a {@code Comparator} for comparing elements 569 * @return a {@code Collector} that produces the minimal value 570 */ 571 public static <T> Collector<T, ?, Optional<T>> 572 minBy(Comparator<? super T> comparator) { 573 return reducing(BinaryOperator.minBy(comparator)); 574 } 575 576 /** 577 * Returns a {@code Collector} that produces the maximal element according 578 * to a given {@code Comparator}, described as an {@code Optional<T>}. 579 * 580 * @implSpec 581 * This produces a result equivalent to: 582 * <pre>{@code 583 * reducing(BinaryOperator.maxBy(comparator)) 584 * }</pre> 585 * 586 * @param <T> the type of the input elements 587 * @param comparator a {@code Comparator} for comparing elements 588 * @return a {@code Collector} that produces the maximal value 589 */ 590 public static <T> Collector<T, ?, Optional<T>> 591 maxBy(Comparator<? super T> comparator) { 592 return reducing(BinaryOperator.maxBy(comparator)); 593 } 594 595 /** 596 * Returns a {@code Collector} that produces the sum of a integer-valued 597 * function applied to the input elements. If no elements are present, 598 * the result is 0. 599 * 600 * @param <T> the type of the input elements 601 * @param mapper a function extracting the property to be summed 602 * @return a {@code Collector} that produces the sum of a derived property 603 */ 604 public static <T> Collector<T, ?, Integer> 605 summingInt(ToIntFunction<? super T> mapper) { 606 return new CollectorImpl<>( 607 () -> new int[1], 608 (a, t) -> { a[0] += mapper.applyAsInt(t); }, 609 (a, b) -> { a[0] += b[0]; return a; }, 610 a -> a[0], CH_NOID); 611 } 612 613 /** 614 * Returns a {@code Collector} that produces the sum of a long-valued 615 * function applied to the input elements. If no elements are present, 616 * the result is 0. 617 * 618 * @param <T> the type of the input elements 619 * @param mapper a function extracting the property to be summed 620 * @return a {@code Collector} that produces the sum of a derived property 621 */ 622 public static <T> Collector<T, ?, Long> 623 summingLong(ToLongFunction<? super T> mapper) { 624 return new CollectorImpl<>( 625 () -> new long[1], 626 (a, t) -> { a[0] += mapper.applyAsLong(t); }, 627 (a, b) -> { a[0] += b[0]; return a; }, 628 a -> a[0], CH_NOID); 629 } 630 631 /** 632 * Returns a {@code Collector} that produces the sum of a double-valued 633 * function applied to the input elements. If no elements are present, 634 * the result is 0. 635 * 636 * <p>The sum returned can vary depending upon the order in which 637 * values are recorded, due to accumulated rounding error in 638 * addition of values of differing magnitudes. Values sorted by increasing 639 * absolute magnitude tend to yield more accurate results. If any recorded 640 * value is a {@code NaN} or the sum is at any point a {@code NaN} then the 641 * sum will be {@code NaN}. 642 * 643 * @param <T> the type of the input elements 644 * @param mapper a function extracting the property to be summed 645 * @return a {@code Collector} that produces the sum of a derived property 646 */ 647 public static <T> Collector<T, ?, Double> 648 summingDouble(ToDoubleFunction<? super T> mapper) { 649 /* 650 * In the arrays allocated for the collect operation, index 0 651 * holds the high-order bits of the running sum, index 1 holds 652 * the low-order bits of the sum computed via compensated 653 * summation, and index 2 holds the simple sum used to compute 654 * the proper result if the stream contains infinite values of 655 * the same sign. 656 */ 657 return new CollectorImpl<>( 658 () -> new double[3], 659 (a, t) -> { double val = mapper.applyAsDouble(t); 660 sumWithCompensation(a, val); 661 a[2] += val;}, 662 (a, b) -> { sumWithCompensation(a, b[0]); 663 a[2] += b[2]; 664 return sumWithCompensation(a, b[1]); }, 665 a -> computeFinalSum(a), 666 CH_NOID); 667 } 668 669 /** 670 * Incorporate a new double value using Kahan summation / 671 * compensation summation. 672 * 673 * High-order bits of the sum are in intermediateSum[0], low-order 674 * bits of the sum are in intermediateSum[1], any additional 675 * elements are application-specific. 676 * 677 * @param intermediateSum the high-order and low-order words of the intermediate sum 678 * @param value the name value to be included in the running sum 679 */ 680 static double[] sumWithCompensation(double[] intermediateSum, double value) { 681 double tmp = value - intermediateSum[1]; 682 double sum = intermediateSum[0]; 683 double velvel = sum + tmp; // Little wolf of rounding error 684 intermediateSum[1] = (velvel - sum) - tmp; 685 intermediateSum[0] = velvel; 686 return intermediateSum; 687 } 688 689 /** 690 * If the compensated sum is spuriously NaN from accumulating one 691 * or more same-signed infinite values, return the 692 * correctly-signed infinity stored in the simple sum. 693 */ 694 static double computeFinalSum(double[] summands) { 695 // Better error bounds to add both terms as the final sum 696 double tmp = summands[0] + summands[1]; 697 double simpleSum = summands[summands.length - 1]; 698 if (Double.isNaN(tmp) && Double.isInfinite(simpleSum)) 699 return simpleSum; 700 else 701 return tmp; 702 } 703 704 /** 705 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued 706 * function applied to the input elements. If no elements are present, 707 * the result is 0. 708 * 709 * @param <T> the type of the input elements 710 * @param mapper a function extracting the property to be summed 711 * @return a {@code Collector} that produces the sum of a derived property 712 */ 713 public static <T> Collector<T, ?, Double> 714 averagingInt(ToIntFunction<? super T> mapper) { 715 return new CollectorImpl<>( 716 () -> new long[2], 717 (a, t) -> { a[0] += mapper.applyAsInt(t); a[1]++; }, 718 (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; }, 719 a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID); 720 } 721 722 /** 723 * Returns a {@code Collector} that produces the arithmetic mean of a long-valued 724 * function applied to the input elements. If no elements are present, 725 * the result is 0. 726 * 727 * @param <T> the type of the input elements 728 * @param mapper a function extracting the property to be summed 729 * @return a {@code Collector} that produces the sum of a derived property 730 */ 731 public static <T> Collector<T, ?, Double> 732 averagingLong(ToLongFunction<? super T> mapper) { 733 return new CollectorImpl<>( 734 () -> new long[2], 735 (a, t) -> { a[0] += mapper.applyAsLong(t); a[1]++; }, 736 (a, b) -> { a[0] += b[0]; a[1] += b[1]; return a; }, 737 a -> (a[1] == 0) ? 0.0d : (double) a[0] / a[1], CH_NOID); 738 } 739 740 /** 741 * Returns a {@code Collector} that produces the arithmetic mean of a double-valued 742 * function applied to the input elements. If no elements are present, 743 * the result is 0. 744 * 745 * <p>The average returned can vary depending upon the order in which 746 * values are recorded, due to accumulated rounding error in 747 * addition of values of differing magnitudes. Values sorted by increasing 748 * absolute magnitude tend to yield more accurate results. If any recorded 749 * value is a {@code NaN} or the sum is at any point a {@code NaN} then the 750 * average will be {@code NaN}. 751 * 752 * @implNote The {@code double} format can represent all 753 * consecutive integers in the range -2<sup>53</sup> to 754 * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup> 755 * values, the divisor in the average computation will saturate at 756 * 2<sup>53</sup>, leading to additional numerical errors. 757 * 758 * @param <T> the type of the input elements 759 * @param mapper a function extracting the property to be summed 760 * @return a {@code Collector} that produces the sum of a derived property 761 */ 762 public static <T> Collector<T, ?, Double> 763 averagingDouble(ToDoubleFunction<? super T> mapper) { 764 /* 765 * In the arrays allocated for the collect operation, index 0 766 * holds the high-order bits of the running sum, index 1 holds 767 * the low-order bits of the sum computed via compensated 768 * summation, and index 2 holds the number of values seen. 769 */ 770 return new CollectorImpl<>( 771 () -> new double[4], 772 (a, t) -> { double val = mapper.applyAsDouble(t); sumWithCompensation(a, val); a[2]++; a[3]+= val;}, 773 (a, b) -> { sumWithCompensation(a, b[0]); sumWithCompensation(a, b[1]); a[2] += b[2]; a[3] += b[3]; return a; }, 774 a -> (a[2] == 0) ? 0.0d : (computeFinalSum(a) / a[2]), 775 CH_NOID); 776 } 777 778 /** 779 * Returns a {@code Collector} which performs a reduction of its 780 * input elements under a specified {@code BinaryOperator} using the 781 * provided identity. 782 * 783 * @apiNote 784 * The {@code reducing()} collectors are most useful when used in a 785 * multi-level reduction, downstream of {@code groupingBy} or 786 * {@code partitioningBy}. To perform a simple reduction on a stream, 787 * use {@link Stream#reduce(Object, BinaryOperator)}} instead. 788 * 789 * @param <T> element type for the input and output of the reduction 790 * @param identity the identity value for the reduction (also, the value 791 * that is returned when there are no input elements) 792 * @param op a {@code BinaryOperator<T>} used to reduce the input elements 793 * @return a {@code Collector} which implements the reduction operation 794 * 795 * @see #reducing(BinaryOperator) 796 * @see #reducing(Object, Function, BinaryOperator) 797 */ 798 public static <T> Collector<T, ?, T> 799 reducing(T identity, BinaryOperator<T> op) { 800 return new CollectorImpl<>( 801 boxSupplier(identity), 802 (a, t) -> { a[0] = op.apply(a[0], t); }, 803 (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, 804 a -> a[0], 805 CH_NOID); 806 } 807 808 @SuppressWarnings("unchecked") 809 private static <T> Supplier<T[]> boxSupplier(T identity) { 810 return () -> (T[]) new Object[] { identity }; 811 } 812 813 /** 814 * Returns a {@code Collector} which performs a reduction of its 815 * input elements under a specified {@code BinaryOperator}. The result 816 * is described as an {@code Optional<T>}. 817 * 818 * @apiNote 819 * The {@code reducing()} collectors are most useful when used in a 820 * multi-level reduction, downstream of {@code groupingBy} or 821 * {@code partitioningBy}. To perform a simple reduction on a stream, 822 * use {@link Stream#reduce(BinaryOperator)} instead. 823 * 824 * <p>For example, given a stream of {@code Person}, to calculate tallest 825 * person in each city: 826 * <pre>{@code 827 * Comparator<Person> byHeight = Comparator.comparing(Person::getHeight); 828 * Map<City, Optional<Person>> tallestByCity 829 * = people.stream().collect(groupingBy(Person::getCity, reducing(BinaryOperator.maxBy(byHeight)))); 830 * }</pre> 831 * 832 * @param <T> element type for the input and output of the reduction 833 * @param op a {@code BinaryOperator<T>} used to reduce the input elements 834 * @return a {@code Collector} which implements the reduction operation 835 * 836 * @see #reducing(Object, BinaryOperator) 837 * @see #reducing(Object, Function, BinaryOperator) 838 */ 839 public static <T> Collector<T, ?, Optional<T>> 840 reducing(BinaryOperator<T> op) { 841 class OptionalBox implements Consumer<T> { 842 T value = null; 843 boolean present = false; 844 845 @Override 846 public void accept(T t) { 847 if (present) { 848 value = op.apply(value, t); 849 } 850 else { 851 value = t; 852 present = true; 853 } 854 } 855 } 856 857 return new CollectorImpl<T, OptionalBox, Optional<T>>( 858 OptionalBox::new, OptionalBox::accept, 859 (a, b) -> { if (b.present) a.accept(b.value); return a; }, 860 a -> Optional.ofNullable(a.value), CH_NOID); 861 } 862 863 /** 864 * Returns a {@code Collector} which performs a reduction of its 865 * input elements under a specified mapping function and 866 * {@code BinaryOperator}. This is a generalization of 867 * {@link #reducing(Object, BinaryOperator)} which allows a transformation 868 * of the elements before reduction. 869 * 870 * @apiNote 871 * The {@code reducing()} collectors are most useful when used in a 872 * multi-level reduction, downstream of {@code groupingBy} or 873 * {@code partitioningBy}. To perform a simple map-reduce on a stream, 874 * use {@link Stream#map(Function)} and {@link Stream#reduce(Object, BinaryOperator)} 875 * instead. 876 * 877 * <p>For example, given a stream of {@code Person}, to calculate the longest 878 * last name of residents in each city: 879 * <pre>{@code 880 * Comparator<String> byLength = Comparator.comparing(String::length); 881 * Map<City, String> longestLastNameByCity 882 * = people.stream().collect(groupingBy(Person::getCity, 883 * reducing("", Person::getLastName, BinaryOperator.maxBy(byLength)))); 884 * }</pre> 885 * 886 * @param <T> the type of the input elements 887 * @param <U> the type of the mapped values 888 * @param identity the identity value for the reduction (also, the value 889 * that is returned when there are no input elements) 890 * @param mapper a mapping function to apply to each input value 891 * @param op a {@code BinaryOperator<U>} used to reduce the mapped values 892 * @return a {@code Collector} implementing the map-reduce operation 893 * 894 * @see #reducing(Object, BinaryOperator) 895 * @see #reducing(BinaryOperator) 896 */ 897 public static <T, U> 898 Collector<T, ?, U> reducing(U identity, 899 Function<? super T, ? extends U> mapper, 900 BinaryOperator<U> op) { 901 return new CollectorImpl<>( 902 boxSupplier(identity), 903 (a, t) -> { a[0] = op.apply(a[0], mapper.apply(t)); }, 904 (a, b) -> { a[0] = op.apply(a[0], b[0]); return a; }, 905 a -> a[0], CH_NOID); 906 } 907 908 /** 909 * Returns a {@code Collector} implementing a "group by" operation on 910 * input elements of type {@code T}, grouping elements according to a 911 * classification function, and returning the results in a {@code Map}. 912 * 913 * <p>The classification function maps elements to some key type {@code K}. 914 * The collector produces a {@code Map<K, List<T>>} whose keys are the 915 * values resulting from applying the classification function to the input 916 * elements, and whose corresponding values are {@code List}s containing the 917 * input elements which map to the associated key under the classification 918 * function. 919 * 920 * <p>There are no guarantees on the type, mutability, serializability, or 921 * thread-safety of the {@code Map} or {@code List} objects returned. 922 * @implSpec 923 * This produces a result similar to: 924 * <pre>{@code 925 * groupingBy(classifier, toList()); 926 * }</pre> 927 * 928 * @implNote 929 * The returned {@code Collector} is not concurrent. For parallel stream 930 * pipelines, the {@code combiner} function operates by merging the keys 931 * from one map into another, which can be an expensive operation. If 932 * preservation of the order in which elements appear in the resulting {@code Map} 933 * collector is not required, using {@link #groupingByConcurrent(Function)} 934 * may offer better parallel performance. 935 * 936 * @param <T> the type of the input elements 937 * @param <K> the type of the keys 938 * @param classifier the classifier function mapping input elements to keys 939 * @return a {@code Collector} implementing the group-by operation 940 * 941 * @see #groupingBy(Function, Collector) 942 * @see #groupingBy(Function, Supplier, Collector) 943 * @see #groupingByConcurrent(Function) 944 */ 945 public static <T, K> Collector<T, ?, Map<K, List<T>>> 946 groupingBy(Function<? super T, ? extends K> classifier) { 947 return groupingBy(classifier, toList()); 948 } 949 950 /** 951 * Returns a {@code Collector} implementing a cascaded "group by" operation 952 * on input elements of type {@code T}, grouping elements according to a 953 * classification function, and then performing a reduction operation on 954 * the values associated with a given key using the specified downstream 955 * {@code Collector}. 956 * 957 * <p>The classification function maps elements to some key type {@code K}. 958 * The downstream collector operates on elements of type {@code T} and 959 * produces a result of type {@code D}. The resulting collector produces a 960 * {@code Map<K, D>}. 961 * 962 * <p>There are no guarantees on the type, mutability, 963 * serializability, or thread-safety of the {@code Map} returned. 964 * 965 * <p>For example, to compute the set of last names of people in each city: 966 * <pre>{@code 967 * Map<City, Set<String>> namesByCity 968 * = people.stream().collect(groupingBy(Person::getCity, 969 * mapping(Person::getLastName, toSet()))); 970 * }</pre> 971 * 972 * @implNote 973 * The returned {@code Collector} is not concurrent. For parallel stream 974 * pipelines, the {@code combiner} function operates by merging the keys 975 * from one map into another, which can be an expensive operation. If 976 * preservation of the order in which elements are presented to the downstream 977 * collector is not required, using {@link #groupingByConcurrent(Function, Collector)} 978 * may offer better parallel performance. 979 * 980 * @param <T> the type of the input elements 981 * @param <K> the type of the keys 982 * @param <A> the intermediate accumulation type of the downstream collector 983 * @param <D> the result type of the downstream reduction 984 * @param classifier a classifier function mapping input elements to keys 985 * @param downstream a {@code Collector} implementing the downstream reduction 986 * @return a {@code Collector} implementing the cascaded group-by operation 987 * @see #groupingBy(Function) 988 * 989 * @see #groupingBy(Function, Supplier, Collector) 990 * @see #groupingByConcurrent(Function, Collector) 991 */ 992 public static <T, K, A, D> 993 Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier, 994 Collector<? super T, A, D> downstream) { 995 return groupingBy(classifier, HashMap::new, downstream); 996 } 997 998 /** 999 * Returns a {@code Collector} implementing a cascaded "group by" operation 1000 * on input elements of type {@code T}, grouping elements according to a 1001 * classification function, and then performing a reduction operation on 1002 * the values associated with a given key using the specified downstream 1003 * {@code Collector}. The {@code Map} produced by the Collector is created 1004 * with the supplied factory function. 1005 * 1006 * <p>The classification function maps elements to some key type {@code K}. 1007 * The downstream collector operates on elements of type {@code T} and 1008 * produces a result of type {@code D}. The resulting collector produces a 1009 * {@code Map<K, D>}. 1010 * 1011 * <p>For example, to compute the set of last names of people in each city, 1012 * where the city names are sorted: 1013 * <pre>{@code 1014 * Map<City, Set<String>> namesByCity 1015 * = people.stream().collect(groupingBy(Person::getCity, TreeMap::new, 1016 * mapping(Person::getLastName, toSet()))); 1017 * }</pre> 1018 * 1019 * @implNote 1020 * The returned {@code Collector} is not concurrent. For parallel stream 1021 * pipelines, the {@code combiner} function operates by merging the keys 1022 * from one map into another, which can be an expensive operation. If 1023 * preservation of the order in which elements are presented to the downstream 1024 * collector is not required, using {@link #groupingByConcurrent(Function, Supplier, Collector)} 1025 * may offer better parallel performance. 1026 * 1027 * @param <T> the type of the input elements 1028 * @param <K> the type of the keys 1029 * @param <A> the intermediate accumulation type of the downstream collector 1030 * @param <D> the result type of the downstream reduction 1031 * @param <M> the type of the resulting {@code Map} 1032 * @param classifier a classifier function mapping input elements to keys 1033 * @param downstream a {@code Collector} implementing the downstream reduction 1034 * @param mapFactory a function which, when called, produces a new empty 1035 * {@code Map} of the desired type 1036 * @return a {@code Collector} implementing the cascaded group-by operation 1037 * 1038 * @see #groupingBy(Function, Collector) 1039 * @see #groupingBy(Function) 1040 * @see #groupingByConcurrent(Function, Supplier, Collector) 1041 */ 1042 public static <T, K, D, A, M extends Map<K, D>> 1043 Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier, 1044 Supplier<M> mapFactory, 1045 Collector<? super T, A, D> downstream) { 1046 Supplier<A> downstreamSupplier = downstream.supplier(); 1047 BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); 1048 BiConsumer<Map<K, A>, T> accumulator = (m, t) -> { 1049 K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); 1050 A container = m.computeIfAbsent(key, k -> downstreamSupplier.get()); 1051 downstreamAccumulator.accept(container, t); 1052 }; 1053 BinaryOperator<Map<K, A>> merger = Collectors.<K, A, Map<K, A>>mapMerger(downstream.combiner()); 1054 @SuppressWarnings("unchecked") 1055 Supplier<Map<K, A>> mangledFactory = (Supplier<Map<K, A>>) mapFactory; 1056 1057 if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { 1058 return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_ID); 1059 } 1060 else { 1061 @SuppressWarnings("unchecked") 1062 Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher(); 1063 Function<Map<K, A>, M> finisher = intermediate -> { 1064 intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v)); 1065 @SuppressWarnings("unchecked") 1066 M castResult = (M) intermediate; 1067 return castResult; 1068 }; 1069 return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_NOID); 1070 } 1071 } 1072 1073 /** 1074 * Returns a concurrent {@code Collector} implementing a "group by" 1075 * operation on input elements of type {@code T}, grouping elements 1076 * according to a classification function. 1077 * 1078 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1079 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1080 * 1081 * <p>The classification function maps elements to some key type {@code K}. 1082 * The collector produces a {@code ConcurrentMap<K, List<T>>} whose keys are the 1083 * values resulting from applying the classification function to the input 1084 * elements, and whose corresponding values are {@code List}s containing the 1085 * input elements which map to the associated key under the classification 1086 * function. 1087 * 1088 * <p>There are no guarantees on the type, mutability, or serializability 1089 * of the {@code ConcurrentMap} or {@code List} objects returned, or of the 1090 * thread-safety of the {@code List} objects returned. 1091 * @implSpec 1092 * This produces a result similar to: 1093 * <pre>{@code 1094 * groupingByConcurrent(classifier, toList()); 1095 * }</pre> 1096 * 1097 * @param <T> the type of the input elements 1098 * @param <K> the type of the keys 1099 * @param classifier a classifier function mapping input elements to keys 1100 * @return a concurrent, unordered {@code Collector} implementing the group-by operation 1101 * 1102 * @see #groupingBy(Function) 1103 * @see #groupingByConcurrent(Function, Collector) 1104 * @see #groupingByConcurrent(Function, Supplier, Collector) 1105 */ 1106 public static <T, K> 1107 Collector<T, ?, ConcurrentMap<K, List<T>>> 1108 groupingByConcurrent(Function<? super T, ? extends K> classifier) { 1109 return groupingByConcurrent(classifier, ConcurrentHashMap::new, toList()); 1110 } 1111 1112 /** 1113 * Returns a concurrent {@code Collector} implementing a cascaded "group by" 1114 * operation on input elements of type {@code T}, grouping elements 1115 * according to a classification function, and then performing a reduction 1116 * operation on the values associated with a given key using the specified 1117 * downstream {@code Collector}. 1118 * 1119 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1120 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1121 * 1122 * <p>The classification function maps elements to some key type {@code K}. 1123 * The downstream collector operates on elements of type {@code T} and 1124 * produces a result of type {@code D}. The resulting collector produces a 1125 * {@code Map<K, D>}. 1126 * 1127 * <p>There are no guarantees on the type, mutability, or serializability 1128 * of the {@code ConcurrentMap} returned. 1129 * 1130 * <p>For example, to compute the set of last names of people in each city, 1131 * where the city names are sorted: 1132 * <pre>{@code 1133 * ConcurrentMap<City, Set<String>> namesByCity 1134 * = people.stream().collect(groupingByConcurrent(Person::getCity, 1135 * mapping(Person::getLastName, toSet()))); 1136 * }</pre> 1137 * 1138 * @param <T> the type of the input elements 1139 * @param <K> the type of the keys 1140 * @param <A> the intermediate accumulation type of the downstream collector 1141 * @param <D> the result type of the downstream reduction 1142 * @param classifier a classifier function mapping input elements to keys 1143 * @param downstream a {@code Collector} implementing the downstream reduction 1144 * @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation 1145 * 1146 * @see #groupingBy(Function, Collector) 1147 * @see #groupingByConcurrent(Function) 1148 * @see #groupingByConcurrent(Function, Supplier, Collector) 1149 */ 1150 public static <T, K, A, D> 1151 Collector<T, ?, ConcurrentMap<K, D>> groupingByConcurrent(Function<? super T, ? extends K> classifier, 1152 Collector<? super T, A, D> downstream) { 1153 return groupingByConcurrent(classifier, ConcurrentHashMap::new, downstream); 1154 } 1155 1156 /** 1157 * Returns a concurrent {@code Collector} implementing a cascaded "group by" 1158 * operation on input elements of type {@code T}, grouping elements 1159 * according to a classification function, and then performing a reduction 1160 * operation on the values associated with a given key using the specified 1161 * downstream {@code Collector}. The {@code ConcurrentMap} produced by the 1162 * Collector is created with the supplied factory function. 1163 * 1164 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1165 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1166 * 1167 * <p>The classification function maps elements to some key type {@code K}. 1168 * The downstream collector operates on elements of type {@code T} and 1169 * produces a result of type {@code D}. The resulting collector produces a 1170 * {@code Map<K, D>}. 1171 * 1172 * <p>For example, to compute the set of last names of people in each city, 1173 * where the city names are sorted: 1174 * <pre>{@code 1175 * ConcurrentMap<City, Set<String>> namesByCity 1176 * = people.stream().collect(groupingBy(Person::getCity, ConcurrentSkipListMap::new, 1177 * mapping(Person::getLastName, toSet()))); 1178 * }</pre> 1179 * 1180 * 1181 * @param <T> the type of the input elements 1182 * @param <K> the type of the keys 1183 * @param <A> the intermediate accumulation type of the downstream collector 1184 * @param <D> the result type of the downstream reduction 1185 * @param <M> the type of the resulting {@code ConcurrentMap} 1186 * @param classifier a classifier function mapping input elements to keys 1187 * @param downstream a {@code Collector} implementing the downstream reduction 1188 * @param mapFactory a function which, when called, produces a new empty 1189 * {@code ConcurrentMap} of the desired type 1190 * @return a concurrent, unordered {@code Collector} implementing the cascaded group-by operation 1191 * 1192 * @see #groupingByConcurrent(Function) 1193 * @see #groupingByConcurrent(Function, Collector) 1194 * @see #groupingBy(Function, Supplier, Collector) 1195 */ 1196 public static <T, K, A, D, M extends ConcurrentMap<K, D>> 1197 Collector<T, ?, M> groupingByConcurrent(Function<? super T, ? extends K> classifier, 1198 Supplier<M> mapFactory, 1199 Collector<? super T, A, D> downstream) { 1200 Supplier<A> downstreamSupplier = downstream.supplier(); 1201 BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); 1202 BinaryOperator<ConcurrentMap<K, A>> merger = Collectors.<K, A, ConcurrentMap<K, A>>mapMerger(downstream.combiner()); 1203 @SuppressWarnings("unchecked") 1204 Supplier<ConcurrentMap<K, A>> mangledFactory = (Supplier<ConcurrentMap<K, A>>) mapFactory; 1205 BiConsumer<ConcurrentMap<K, A>, T> accumulator; 1206 if (downstream.characteristics().contains(Collector.Characteristics.CONCURRENT)) { 1207 accumulator = (m, t) -> { 1208 K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); 1209 A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get()); 1210 downstreamAccumulator.accept(resultContainer, t); 1211 }; 1212 } 1213 else { 1214 accumulator = (m, t) -> { 1215 K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key"); 1216 A resultContainer = m.computeIfAbsent(key, k -> downstreamSupplier.get()); 1217 synchronized (resultContainer) { 1218 downstreamAccumulator.accept(resultContainer, t); 1219 } 1220 }; 1221 } 1222 1223 if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { 1224 return new CollectorImpl<>(mangledFactory, accumulator, merger, CH_CONCURRENT_ID); 1225 } 1226 else { 1227 @SuppressWarnings("unchecked") 1228 Function<A, A> downstreamFinisher = (Function<A, A>) downstream.finisher(); 1229 Function<ConcurrentMap<K, A>, M> finisher = intermediate -> { 1230 intermediate.replaceAll((k, v) -> downstreamFinisher.apply(v)); 1231 @SuppressWarnings("unchecked") 1232 M castResult = (M) intermediate; 1233 return castResult; 1234 }; 1235 return new CollectorImpl<>(mangledFactory, accumulator, merger, finisher, CH_CONCURRENT_NOID); 1236 } 1237 } 1238 1239 /** 1240 * Returns a {@code Collector} which partitions the input elements according 1241 * to a {@code Predicate}, and organizes them into a 1242 * {@code Map<Boolean, List<T>>}. 1243 * 1244 * There are no guarantees on the type, mutability, 1245 * serializability, or thread-safety of the {@code Map} or {@code List} 1246 * returned. 1247 * 1248 * @param <T> the type of the input elements 1249 * @param predicate a predicate used for classifying input elements 1250 * @return a {@code Collector} implementing the partitioning operation 1251 * 1252 * @see #partitioningBy(Predicate, Collector) 1253 */ 1254 public static <T> 1255 Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) { 1256 return partitioningBy(predicate, toList()); 1257 } 1258 1259 /** 1260 * Returns a {@code Collector} which partitions the input elements according 1261 * to a {@code Predicate}, reduces the values in each partition according to 1262 * another {@code Collector}, and organizes them into a 1263 * {@code Map<Boolean, D>} whose values are the result of the downstream 1264 * reduction. 1265 * 1266 * <p>There are no guarantees on the type, mutability, 1267 * serializability, or thread-safety of the {@code Map} returned. 1268 * 1269 * @param <T> the type of the input elements 1270 * @param <A> the intermediate accumulation type of the downstream collector 1271 * @param <D> the result type of the downstream reduction 1272 * @param predicate a predicate used for classifying input elements 1273 * @param downstream a {@code Collector} implementing the downstream 1274 * reduction 1275 * @return a {@code Collector} implementing the cascaded partitioning 1276 * operation 1277 * 1278 * @see #partitioningBy(Predicate) 1279 */ 1280 public static <T, D, A> 1281 Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate, 1282 Collector<? super T, A, D> downstream) { 1283 BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator(); 1284 BiConsumer<Partition<A>, T> accumulator = (result, t) -> 1285 downstreamAccumulator.accept(predicate.test(t) ? result.forTrue : result.forFalse, t); 1286 BinaryOperator<A> op = downstream.combiner(); 1287 BinaryOperator<Partition<A>> merger = (left, right) -> 1288 new Partition<>(op.apply(left.forTrue, right.forTrue), 1289 op.apply(left.forFalse, right.forFalse)); 1290 Supplier<Partition<A>> supplier = () -> 1291 new Partition<>(downstream.supplier().get(), 1292 downstream.supplier().get()); 1293 if (downstream.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) { 1294 return new CollectorImpl<>(supplier, accumulator, merger, CH_ID); 1295 } 1296 else { 1297 Function<Partition<A>, Map<Boolean, D>> finisher = par -> 1298 new Partition<>(downstream.finisher().apply(par.forTrue), 1299 downstream.finisher().apply(par.forFalse)); 1300 return new CollectorImpl<>(supplier, accumulator, merger, finisher, CH_NOID); 1301 } 1302 } 1303 1304 /** 1305 * Returns a {@code Collector} that accumulates elements into a 1306 * {@code Map} whose keys and values are the result of applying the provided 1307 * mapping functions to the input elements. 1308 * 1309 * <p>If the mapped keys contains duplicates (according to 1310 * {@link Object#equals(Object)}), an {@code IllegalStateException} is 1311 * thrown when the collection operation is performed. If the mapped keys 1312 * may have duplicates, use {@link #toMap(Function, Function, BinaryOperator)} 1313 * instead. 1314 * 1315 * <p>There are no guarantees on the type, mutability, serializability, 1316 * or thread-safety of the {@code Map} returned. 1317 * 1318 * @apiNote 1319 * It is common for either the key or the value to be the input elements. 1320 * In this case, the utility method 1321 * {@link java.util.function.Function#identity()} may be helpful. 1322 * For example, the following produces a {@code Map} mapping 1323 * students to their grade point average: 1324 * <pre>{@code 1325 * Map<Student, Double> studentToGPA 1326 * students.stream().collect(toMap(Function.identity(), 1327 * student -> computeGPA(student))); 1328 * }</pre> 1329 * And the following produces a {@code Map} mapping a unique identifier to 1330 * students: 1331 * <pre>{@code 1332 * Map<String, Student> studentIdToStudent 1333 * students.stream().collect(toMap(Student::getId, 1334 * Function.identity()); 1335 * }</pre> 1336 * 1337 * @implNote 1338 * The returned {@code Collector} is not concurrent. For parallel stream 1339 * pipelines, the {@code combiner} function operates by merging the keys 1340 * from one map into another, which can be an expensive operation. If it is 1341 * not required that results are inserted into the {@code Map} in encounter 1342 * order, using {@link #toConcurrentMap(Function, Function)} 1343 * may offer better parallel performance. 1344 * 1345 * @param <T> the type of the input elements 1346 * @param <K> the output type of the key mapping function 1347 * @param <U> the output type of the value mapping function 1348 * @param keyMapper a mapping function to produce keys 1349 * @param valueMapper a mapping function to produce values 1350 * @return a {@code Collector} which collects elements into a {@code Map} 1351 * whose keys and values are the result of applying mapping functions to 1352 * the input elements 1353 * 1354 * @see #toMap(Function, Function, BinaryOperator) 1355 * @see #toMap(Function, Function, BinaryOperator, Supplier) 1356 * @see #toConcurrentMap(Function, Function) 1357 */ 1358 public static <T, K, U> 1359 Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, 1360 Function<? super T, ? extends U> valueMapper) { 1361 return new CollectorImpl<>(HashMap::new, 1362 uniqKeysMapAccumulator(keyMapper, valueMapper), 1363 uniqKeysMapMerger(), 1364 CH_ID); 1365 } 1366 1367 /** 1368 * Returns a {@code Collector} that accumulates elements into a 1369 * {@code Map} whose keys and values are the result of applying the provided 1370 * mapping functions to the input elements. 1371 * 1372 * <p>If the mapped 1373 * keys contains duplicates (according to {@link Object#equals(Object)}), 1374 * the value mapping function is applied to each equal element, and the 1375 * results are merged using the provided merging function. 1376 * 1377 * <p>There are no guarantees on the type, mutability, serializability, 1378 * or thread-safety of the {@code Map} returned. 1379 * 1380 * @apiNote 1381 * There are multiple ways to deal with collisions between multiple elements 1382 * mapping to the same key. The other forms of {@code toMap} simply use 1383 * a merge function that throws unconditionally, but you can easily write 1384 * more flexible merge policies. For example, if you have a stream 1385 * of {@code Person}, and you want to produce a "phone book" mapping name to 1386 * address, but it is possible that two persons have the same name, you can 1387 * do as follows to gracefully deals with these collisions, and produce a 1388 * {@code Map} mapping names to a concatenated list of addresses: 1389 * <pre>{@code 1390 * Map<String, String> phoneBook 1391 * people.stream().collect(toMap(Person::getName, 1392 * Person::getAddress, 1393 * (s, a) -> s + ", " + a)); 1394 * }</pre> 1395 * 1396 * @implNote 1397 * The returned {@code Collector} is not concurrent. For parallel stream 1398 * pipelines, the {@code combiner} function operates by merging the keys 1399 * from one map into another, which can be an expensive operation. If it is 1400 * not required that results are merged into the {@code Map} in encounter 1401 * order, using {@link #toConcurrentMap(Function, Function, BinaryOperator)} 1402 * may offer better parallel performance. 1403 * 1404 * @param <T> the type of the input elements 1405 * @param <K> the output type of the key mapping function 1406 * @param <U> the output type of the value mapping function 1407 * @param keyMapper a mapping function to produce keys 1408 * @param valueMapper a mapping function to produce values 1409 * @param mergeFunction a merge function, used to resolve collisions between 1410 * values associated with the same key, as supplied 1411 * to {@link Map#merge(Object, Object, BiFunction)} 1412 * @return a {@code Collector} which collects elements into a {@code Map} 1413 * whose keys are the result of applying a key mapping function to the input 1414 * elements, and whose values are the result of applying a value mapping 1415 * function to all input elements equal to the key and combining them 1416 * using the merge function 1417 * 1418 * @see #toMap(Function, Function) 1419 * @see #toMap(Function, Function, BinaryOperator, Supplier) 1420 * @see #toConcurrentMap(Function, Function, BinaryOperator) 1421 */ 1422 public static <T, K, U> 1423 Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, 1424 Function<? super T, ? extends U> valueMapper, 1425 BinaryOperator<U> mergeFunction) { 1426 return toMap(keyMapper, valueMapper, mergeFunction, HashMap::new); 1427 } 1428 1429 /** 1430 * Returns a {@code Collector} that accumulates elements into a 1431 * {@code Map} whose keys and values are the result of applying the provided 1432 * mapping functions to the input elements. 1433 * 1434 * <p>If the mapped 1435 * keys contains duplicates (according to {@link Object#equals(Object)}), 1436 * the value mapping function is applied to each equal element, and the 1437 * results are merged using the provided merging function. The {@code Map} 1438 * is created by a provided supplier function. 1439 * 1440 * @implNote 1441 * The returned {@code Collector} is not concurrent. For parallel stream 1442 * pipelines, the {@code combiner} function operates by merging the keys 1443 * from one map into another, which can be an expensive operation. If it is 1444 * not required that results are merged into the {@code Map} in encounter 1445 * order, using {@link #toConcurrentMap(Function, Function, BinaryOperator, Supplier)} 1446 * may offer better parallel performance. 1447 * 1448 * @param <T> the type of the input elements 1449 * @param <K> the output type of the key mapping function 1450 * @param <U> the output type of the value mapping function 1451 * @param <M> the type of the resulting {@code Map} 1452 * @param keyMapper a mapping function to produce keys 1453 * @param valueMapper a mapping function to produce values 1454 * @param mergeFunction a merge function, used to resolve collisions between 1455 * values associated with the same key, as supplied 1456 * to {@link Map#merge(Object, Object, BiFunction)} 1457 * @param mapSupplier a function which returns a new, empty {@code Map} into 1458 * which the results will be inserted 1459 * @return a {@code Collector} which collects elements into a {@code Map} 1460 * whose keys are the result of applying a key mapping function to the input 1461 * elements, and whose values are the result of applying a value mapping 1462 * function to all input elements equal to the key and combining them 1463 * using the merge function 1464 * 1465 * @see #toMap(Function, Function) 1466 * @see #toMap(Function, Function, BinaryOperator) 1467 * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) 1468 */ 1469 public static <T, K, U, M extends Map<K, U>> 1470 Collector<T, ?, M> toMap(Function<? super T, ? extends K> keyMapper, 1471 Function<? super T, ? extends U> valueMapper, 1472 BinaryOperator<U> mergeFunction, 1473 Supplier<M> mapSupplier) { 1474 BiConsumer<M, T> accumulator 1475 = (map, element) -> map.merge(keyMapper.apply(element), 1476 valueMapper.apply(element), mergeFunction); 1477 return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_ID); 1478 } 1479 1480 /** 1481 * Returns a concurrent {@code Collector} that accumulates elements into a 1482 * {@code ConcurrentMap} whose keys and values are the result of applying 1483 * the provided mapping functions to the input elements. 1484 * 1485 * <p>If the mapped keys contains duplicates (according to 1486 * {@link Object#equals(Object)}), an {@code IllegalStateException} is 1487 * thrown when the collection operation is performed. If the mapped keys 1488 * may have duplicates, use 1489 * {@link #toConcurrentMap(Function, Function, BinaryOperator)} instead. 1490 * 1491 * <p>There are no guarantees on the type, mutability, or serializability 1492 * of the {@code ConcurrentMap} returned. 1493 * 1494 * @apiNote 1495 * It is common for either the key or the value to be the input elements. 1496 * In this case, the utility method 1497 * {@link java.util.function.Function#identity()} may be helpful. 1498 * For example, the following produces a {@code Map} mapping 1499 * students to their grade point average: 1500 * <pre>{@code 1501 * Map<Student, Double> studentToGPA 1502 * students.stream().collect(toMap(Function.identity(), 1503 * student -> computeGPA(student))); 1504 * }</pre> 1505 * And the following produces a {@code Map} mapping a unique identifier to 1506 * students: 1507 * <pre>{@code 1508 * Map<String, Student> studentIdToStudent 1509 * students.stream().collect(toConcurrentMap(Student::getId, 1510 * Function.identity()); 1511 * }</pre> 1512 * 1513 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1514 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1515 * 1516 * @param <T> the type of the input elements 1517 * @param <K> the output type of the key mapping function 1518 * @param <U> the output type of the value mapping function 1519 * @param keyMapper the mapping function to produce keys 1520 * @param valueMapper the mapping function to produce values 1521 * @return a concurrent, unordered {@code Collector} which collects elements into a 1522 * {@code ConcurrentMap} whose keys are the result of applying a key mapping 1523 * function to the input elements, and whose values are the result of 1524 * applying a value mapping function to the input elements 1525 * 1526 * @see #toMap(Function, Function) 1527 * @see #toConcurrentMap(Function, Function, BinaryOperator) 1528 * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) 1529 */ 1530 public static <T, K, U> 1531 Collector<T, ?, ConcurrentMap<K,U>> toConcurrentMap(Function<? super T, ? extends K> keyMapper, 1532 Function<? super T, ? extends U> valueMapper) { 1533 return new CollectorImpl<>(ConcurrentHashMap::new, 1534 uniqKeysMapAccumulator(keyMapper, valueMapper), 1535 uniqKeysMapMerger(), 1536 CH_CONCURRENT_ID); 1537 } 1538 1539 /** 1540 * Returns a concurrent {@code Collector} that accumulates elements into a 1541 * {@code ConcurrentMap} whose keys and values are the result of applying 1542 * the provided mapping functions to the input elements. 1543 * 1544 * <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}), 1545 * the value mapping function is applied to each equal element, and the 1546 * results are merged using the provided merging function. 1547 * 1548 * <p>There are no guarantees on the type, mutability, or serializability 1549 * of the {@code ConcurrentMap} returned. 1550 * 1551 * @apiNote 1552 * There are multiple ways to deal with collisions between multiple elements 1553 * mapping to the same key. The other forms of {@code toConcurrentMap} simply use 1554 * a merge function that throws unconditionally, but you can easily write 1555 * more flexible merge policies. For example, if you have a stream 1556 * of {@code Person}, and you want to produce a "phone book" mapping name to 1557 * address, but it is possible that two persons have the same name, you can 1558 * do as follows to gracefully deals with these collisions, and produce a 1559 * {@code Map} mapping names to a concatenated list of addresses: 1560 * <pre>{@code 1561 * Map<String, String> phoneBook 1562 * people.stream().collect(toConcurrentMap(Person::getName, 1563 * Person::getAddress, 1564 * (s, a) -> s + ", " + a)); 1565 * }</pre> 1566 * 1567 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1568 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1569 * 1570 * @param <T> the type of the input elements 1571 * @param <K> the output type of the key mapping function 1572 * @param <U> the output type of the value mapping function 1573 * @param keyMapper a mapping function to produce keys 1574 * @param valueMapper a mapping function to produce values 1575 * @param mergeFunction a merge function, used to resolve collisions between 1576 * values associated with the same key, as supplied 1577 * to {@link Map#merge(Object, Object, BiFunction)} 1578 * @return a concurrent, unordered {@code Collector} which collects elements into a 1579 * {@code ConcurrentMap} whose keys are the result of applying a key mapping 1580 * function to the input elements, and whose values are the result of 1581 * applying a value mapping function to all input elements equal to the key 1582 * and combining them using the merge function 1583 * 1584 * @see #toConcurrentMap(Function, Function) 1585 * @see #toConcurrentMap(Function, Function, BinaryOperator, Supplier) 1586 * @see #toMap(Function, Function, BinaryOperator) 1587 */ 1588 public static <T, K, U> 1589 Collector<T, ?, ConcurrentMap<K,U>> 1590 toConcurrentMap(Function<? super T, ? extends K> keyMapper, 1591 Function<? super T, ? extends U> valueMapper, 1592 BinaryOperator<U> mergeFunction) { 1593 return toConcurrentMap(keyMapper, valueMapper, mergeFunction, ConcurrentHashMap::new); 1594 } 1595 1596 /** 1597 * Returns a concurrent {@code Collector} that accumulates elements into a 1598 * {@code ConcurrentMap} whose keys and values are the result of applying 1599 * the provided mapping functions to the input elements. 1600 * 1601 * <p>If the mapped keys contains duplicates (according to {@link Object#equals(Object)}), 1602 * the value mapping function is applied to each equal element, and the 1603 * results are merged using the provided merging function. The 1604 * {@code ConcurrentMap} is created by a provided supplier function. 1605 * 1606 * <p>This is a {@link Collector.Characteristics#CONCURRENT concurrent} and 1607 * {@link Collector.Characteristics#UNORDERED unordered} Collector. 1608 * 1609 * @param <T> the type of the input elements 1610 * @param <K> the output type of the key mapping function 1611 * @param <U> the output type of the value mapping function 1612 * @param <M> the type of the resulting {@code ConcurrentMap} 1613 * @param keyMapper a mapping function to produce keys 1614 * @param valueMapper a mapping function to produce values 1615 * @param mergeFunction a merge function, used to resolve collisions between 1616 * values associated with the same key, as supplied 1617 * to {@link Map#merge(Object, Object, BiFunction)} 1618 * @param mapSupplier a function which returns a new, empty {@code Map} into 1619 * which the results will be inserted 1620 * @return a concurrent, unordered {@code Collector} which collects elements into a 1621 * {@code ConcurrentMap} whose keys are the result of applying a key mapping 1622 * function to the input elements, and whose values are the result of 1623 * applying a value mapping function to all input elements equal to the key 1624 * and combining them using the merge function 1625 * 1626 * @see #toConcurrentMap(Function, Function) 1627 * @see #toConcurrentMap(Function, Function, BinaryOperator) 1628 * @see #toMap(Function, Function, BinaryOperator, Supplier) 1629 */ 1630 public static <T, K, U, M extends ConcurrentMap<K, U>> 1631 Collector<T, ?, M> toConcurrentMap(Function<? super T, ? extends K> keyMapper, 1632 Function<? super T, ? extends U> valueMapper, 1633 BinaryOperator<U> mergeFunction, 1634 Supplier<M> mapSupplier) { 1635 BiConsumer<M, T> accumulator 1636 = (map, element) -> map.merge(keyMapper.apply(element), 1637 valueMapper.apply(element), mergeFunction); 1638 return new CollectorImpl<>(mapSupplier, accumulator, mapMerger(mergeFunction), CH_CONCURRENT_ID); 1639 } 1640 1641 /** 1642 * Returns a {@code Collector} which applies an {@code int}-producing 1643 * mapping function to each input element, and returns summary statistics 1644 * for the resulting values. 1645 * 1646 * @param <T> the type of the input elements 1647 * @param mapper a mapping function to apply to each element 1648 * @return a {@code Collector} implementing the summary-statistics reduction 1649 * 1650 * @see #summarizingDouble(ToDoubleFunction) 1651 * @see #summarizingLong(ToLongFunction) 1652 */ 1653 public static <T> 1654 Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) { 1655 return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>( 1656 IntSummaryStatistics::new, 1657 (r, t) -> r.accept(mapper.applyAsInt(t)), 1658 (l, r) -> { l.combine(r); return l; }, CH_ID); 1659 } 1660 1661 /** 1662 * Returns a {@code Collector} which applies an {@code long}-producing 1663 * mapping function to each input element, and returns summary statistics 1664 * for the resulting values. 1665 * 1666 * @param <T> the type of the input elements 1667 * @param mapper the mapping function to apply to each element 1668 * @return a {@code Collector} implementing the summary-statistics reduction 1669 * 1670 * @see #summarizingDouble(ToDoubleFunction) 1671 * @see #summarizingInt(ToIntFunction) 1672 */ 1673 public static <T> 1674 Collector<T, ?, LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) { 1675 return new CollectorImpl<T, LongSummaryStatistics, LongSummaryStatistics>( 1676 LongSummaryStatistics::new, 1677 (r, t) -> r.accept(mapper.applyAsLong(t)), 1678 (l, r) -> { l.combine(r); return l; }, CH_ID); 1679 } 1680 1681 /** 1682 * Returns a {@code Collector} which applies an {@code double}-producing 1683 * mapping function to each input element, and returns summary statistics 1684 * for the resulting values. 1685 * 1686 * @param <T> the type of the input elements 1687 * @param mapper a mapping function to apply to each element 1688 * @return a {@code Collector} implementing the summary-statistics reduction 1689 * 1690 * @see #summarizingLong(ToLongFunction) 1691 * @see #summarizingInt(ToIntFunction) 1692 */ 1693 public static <T> 1694 Collector<T, ?, DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) { 1695 return new CollectorImpl<T, DoubleSummaryStatistics, DoubleSummaryStatistics>( 1696 DoubleSummaryStatistics::new, 1697 (r, t) -> r.accept(mapper.applyAsDouble(t)), 1698 (l, r) -> { l.combine(r); return l; }, CH_ID); 1699 } 1700 1701 /** 1702 * Implementation class used by partitioningBy. 1703 */ 1704 private static final class Partition<T> 1705 extends AbstractMap<Boolean, T> 1706 implements Map<Boolean, T> { 1707 final T forTrue; 1708 final T forFalse; 1709 1710 Partition(T forTrue, T forFalse) { 1711 this.forTrue = forTrue; 1712 this.forFalse = forFalse; 1713 } 1714 1715 @Override 1716 public Set<Map.Entry<Boolean, T>> entrySet() { 1717 return new AbstractSet<Map.Entry<Boolean, T>>() { 1718 @Override 1719 public Iterator<Map.Entry<Boolean, T>> iterator() { 1720 Map.Entry<Boolean, T> falseEntry = new SimpleImmutableEntry<>(false, forFalse); 1721 Map.Entry<Boolean, T> trueEntry = new SimpleImmutableEntry<>(true, forTrue); 1722 return Arrays.asList(falseEntry, trueEntry).iterator(); 1723 } 1724 1725 @Override 1726 public int size() { 1727 return 2; 1728 } 1729 }; 1730 } 1731 } 1732 }