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

Misleading exception message from Collectors.toMap()

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      for test example:
        @Test
        public void test() {
          List<String> strings = Arrays.asList("one","two");
          Map<String, String> collect = strings.stream().collect(Collectors.toMap(o -> "test", String::toString));
        }
      Exectuion will throw "java.lang.IllegalStateException: Duplicate key one"


      JUSTIFICATION :
      This message is misleading because developer could think that key "one" is duplicated in result of Collectors.toMap. There is no information about which key is duplicated or which values have the same key. This problem occured in my team when we compute complicated models with many stream operations.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Message should inform which key is duplicated for example "Duplicate key: test" or which values have the same key: "Values one and two have the same key: test"
      ACTUAL -
      Exception message is java.lang.IllegalStateException: Duplicate key one


      ---------- BEGIN SOURCE ----------
        @Test
        public void test() {
          List<String> strings = Arrays.asList("one","two");
          Map<String, String> collect = strings.stream().collect(Collectors.toMap(o -> "test", String::toString));
        }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: