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

Wrong exception message when collecting a stream to a map

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      I have tried to convert a stream into a map. Unfortunately there were duplicated values in keys. An exception was thrown, which is OK. The only problem is that this exception message points not to the duplicated key, but to the associated value.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Make a stream of some objects with two properties A and B.
      2. Ensure that this stream contains two objects with the same value for A property.
      3. Convert this stream to a map using A property as a key.
      4. Check wrong exception message.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      java.lang.IllegalStateException: Duplicate key KEY1
      ACTUAL -
      java.lang.IllegalStateException: Duplicate key VALUE

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.IllegalStateException: Duplicate key VALUE

      at java.util.stream.Collectors.lambda$throwingMerger$113(Collectors.java:133)
      at java.util.stream.Collectors$$Lambda$3/1361960727.apply(Unknown Source)
      at java.util.HashMap.merge(HashMap.java:1245)
      at java.util.stream.Collectors.lambda$toMap$171(Collectors.java:1320)
      at java.util.stream.Collectors$$Lambda$5/2085857771.accept(Unknown Source)
      at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
      at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
      at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
      at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
      at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
      at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
      at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          @Test
          public void test() {
              List<Pair<String, String>> list = new ArrayList<>();
              list.add(new Pair<>("KEY1", "VALUE"));
              list.add(new Pair<>("KEY1", "VALUE"));
              try {
                  list.stream().collect(Collectors.toMap(Pair::getKey, Pair::getValue));
              } catch (IllegalStateException e) {
                  assertThat(e.getMessage()).isEqualTo("Duplicate key KEY1");
              }
          }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                psonal Pallavi Sonal
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: