Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8199846

toMap uses merging function as remapping function in Map.merge

XMLWordPrintable

      FULL PRODUCT VERSION :
      jdk1.8.0_161.jdk

      A DESCRIPTION OF THE PROBLEM :
      toMap with an empty merge calls Map.merge using the throwingMerger function object as the remappingFunction for value fields. This causes a IllegalStateException on a duplicate key field that is actually the value field.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a list of n Strings where each String is the same value.
      Convert the list into a stream.
      Collect the list using the toMap function and supply no merger function to it.

      List<String> list = Arrays.asList("a", "a", "a");
      list.stream().collect(UUID.randomUUID(), String::toString)

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      {UUID1 -> "a", UUID2 -> "a", UUID3 -> "a"}
      ACTUAL -
      java.lang.IllegalStateException: Duplicate key

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      t] Caused by: java.lang.IllegalStateException: Duplicate key a
          [junit] at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
          [junit] at java.util.HashMap.merge(HashMap.java:1254)
          [junit] at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
          [junit] at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
          [junit] at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
          [junit] at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1625)
          [junit] at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
          [junit] at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
          [junit] at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
          [junit] at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
          [junit] at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      use a simple merge function (a, b) -> a

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: