4949import java .util .IntSummaryStatistics ;
5050import java .util .LinkedHashMap ;
5151import java .util .List ;
52+ import java .util .Locale ;
5253import java .util .LongSummaryStatistics ;
5354import java .util .Map ;
5455import java .util .Optional ;
@@ -406,12 +407,13 @@ public void testList() {
406407 }
407408
408409 public void testMap () {
409- Collector <String , ?, Map <String , String >> c = toMap (Function .identity (), Function .identity ());
410+ Function <String , String > toUpper = s -> s .toUpperCase (Locale .ROOT );
411+ Collector <String , ?, Map <String , String >> c = toMap (Function .identity (), toUpper );
410412
411413 // two distinct items
412414 Map <String , String > map = new HashMap <>();
413- map .put ("a" , "a " );
414- map .put ("b" , "b " );
415+ map .put ("a" , "A " );
416+ map .put ("b" , "B " );
415417 applyItems (map , c , "a" , "b" );
416418
417419 // inline applyItems and test each to confirm failure for duplicates
@@ -425,10 +427,11 @@ public void testMap() {
425427 applyItemsWithSplitting (c , "a" , "a" );
426428 fail ("expected IllegalStateException" );
427429 } catch (IllegalStateException expected ) {
430+ assertEquals ("Duplicate key a" , expected .getMessage ());
428431 }
429432
430433 assertZeroItemsCollectedAs (Collections .emptyMap (), c );
431- assertSingleItemCollectedAs (Collections .singletonMap ("a" , "a " ), c , "a" );
434+ assertSingleItemCollectedAs (Collections .singletonMap ("a" , "A " ), c , "a" );
432435
433436 List <String > seen = new ArrayList <>();
434437 c = toMap (Function .identity (), Function .identity (), (s , s2 ) -> {
@@ -442,6 +445,24 @@ public void testMap() {
442445 assertEquals (Arrays .asList ("first: a" , "second: a" , "first: a" , "second: a" ), seen );
443446 }
444447
448+ public void testMapInvalid () {
449+ Collector <String , ?, Map <String , String >> c = toMap (Function .identity (), ignore -> null );
450+
451+ // inline applyItems and test each to confirm failure for duplicates
452+ try {
453+ applyItemsWithoutSplitting (c , "a" , "b" );
454+ fail ("expected NullPointerException" );
455+ } catch (NullPointerException expected ) {
456+ assertEquals ("Duplicate key a" , expected .getMessage ());
457+ }
458+ try {
459+ applyItemsWithSplitting (c , "a" , "a" );
460+ fail ("expected NullPointerException" );
461+ } catch (NullPointerException expected ) {
462+ assertEquals ("Duplicate key a" , expected .getMessage ());
463+ }
464+ }
465+
445466 public void testSet () {
446467 Collector <String , ?, Set <String >> c = toSet ();
447468
0 commit comments