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