@@ -376,31 +376,11 @@ public static <T> Collector<T,?,List<T>> toList() {
376376 public static <T , K , U > Collector <T , ?, Map <K , U >> toMap (
377377 final Function <? super T , ? extends K > keyMapper ,
378378 final Function <? super T , ? extends U > valueMapper ) {
379- return Collector .of (
380- HashMap ::new ,
381- (map , item ) -> {
382- K key = keyMapper .apply (item );
383- U newValue = Objects .requireNonNull (valueMapper .apply (item ));
384- if (map .containsKey (key )) {
385- throw getDuplicateKeyException (key );
386- } else {
387- map .put (key , newValue );
388- }
389- },
390- (m1 , m2 ) -> {
391- for (Map .Entry <K , U > entry : m2 .entrySet ()) {
392- if (m1 .get (entry .getKey ()) != null ) {
393- throw getDuplicateKeyException (entry .getKey ());
394- }
395- m1 .put (entry .getKey (), entry .getValue ());
396- }
397- return m1 ;
398- },
399- Collector .Characteristics .IDENTITY_FINISH );
379+ return toMap (keyMapper , valueMapper , null , HashMap ::new , true );
400380 }
401381
402382 private static RuntimeException getDuplicateKeyException (Object key ) {
403- return new IllegalStateException ("Duplicate key: " + key );
383+ return new IllegalStateException ("Duplicate key " + key );
404384 }
405385
406386 public static <T , K , U > Collector <T , ?, Map <K , U >> toMap (
@@ -438,18 +418,32 @@ private static <T, R> Function<T, R> disallowNulls(Function<T, R> func) {
438418 final Function <? super T , ? extends U > valueMapper ,
439419 final BinaryOperator <U > mergeFunction ,
440420 final Supplier <M > mapSupplier ) {
421+ return toMap (keyMapper , valueMapper , mergeFunction , mapSupplier , false );
422+ }
423+
424+ /*
425+ * If unique flag is true, mergeFunction can safely be null.
426+ */
427+ private static <T , K , U , M extends Map <K , U >> Collector <T , ?, M > toMap (
428+ final Function <? super T , ? extends K > keyMapper ,
429+ final Function <? super T , ? extends U > valueMapper ,
430+ final BinaryOperator <U > mergeFunction ,
431+ final Supplier <M > mapSupplier , final boolean unique ) {
441432 return Collector .of (
442433 mapSupplier ,
443434 (map , item ) -> {
444435 K key = keyMapper .apply (item );
445436 U newValue = Objects .requireNonNull (valueMapper .apply (item ));
446437 if (map .containsKey (key )) {
438+ if (unique ) {
439+ throw getDuplicateKeyException (key );
440+ }
447441 map .put (key , mergeFunction .apply (map .get (key ), newValue ));
448442 } else {
449443 map .put (key , newValue );
450444 }
451445 },
452- (m1 , m2 ) -> mergeAll (m1 , m2 , mergeFunction ),
446+ (m1 , m2 ) -> unique ? mergeAllUnique ( m1 , m2 ) : mergeAll (m1 , m2 , mergeFunction ),
453447 Collector .Characteristics .IDENTITY_FINISH );
454448 }
455449
@@ -486,6 +480,17 @@ private static <K, V, M extends Map<K, V>> M mergeAll(
486480 return m1 ;
487481 }
488482
483+ private static <K , V , M extends Map <K , V >> M mergeAllUnique (
484+ M m1 , M m2 ) {
485+ for (Map .Entry <K , V > entry : m2 .entrySet ()) {
486+ if (m1 .get (entry .getKey ()) != null ) {
487+ throw getDuplicateKeyException (entry .getKey ());
488+ }
489+ m1 .put (entry .getKey (), entry .getValue ());
490+ }
491+ return m1 ;
492+ }
493+
489494 private static <T , C extends Collection <T >> C addAll (C collection , Collection <T > items ) {
490495 collection .addAll (items );
491496 return collection ;
0 commit comments