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

Collectors.toMap(keyMapper, valueMapper) has incorrect message in exception thrown on a duplicate key

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_121"
      Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux workstation 4.4.0-66-generic #87~14.04.1-Ubuntu SMP Fri Mar 3 17:32:36 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      When using Collectors.toMap(keyMapper, valueMapper), a duplicate key is expected to throw an exception. The exception is thrown. However, the message incorrectly reports a duplicate key when it is reporting the existing value stored at a particular key.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
       Run something like this:

      Arrays.stream(new Character[]{'a', 'b', 'a'})
        .collect(Collectors.toMap(Function.identity(), Character::toUpperCase));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An IllegalStateException with a sensible message.
      ACTUAL -
      The following exception message:
      java.lang.IllegalStateException: Duplicate key A

      Note that 'A' is a value in the map not the key, which would have been 'a'.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class DuplicateKey{

        public static void main(String[] args) {
          Arrays.stream(new Character[]{'a', 'b', 'a'})
              .collect(Collectors.toMap(Function.identity(), Character::toUpperCase));
        }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: