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

ConcurrentHashMap.KeySetView allows incorrect type keys to be added

    Details

      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 :
      OSX 10.9.5

      A DESCRIPTION OF THE PROBLEM :
      Let's say I have ConcurrentHashMap defined like below, by using KeySetView I can add any type of key irrespective of type(Which is Integer in this case) used in definition.




      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>();

      //Add Some thing
      chm.put(1, "Number");

      KeySetView kv = chm.keySet("SomeVAL");

      //add boolean key
      kv.add(true);
      //add string key
      kv.add("SomeString");
      System.out.println(chm);

      //OutPut
      {1=Number, 2=String, 3=SomeVAl, 4=String, SomeString=SomeVAl, true=SomeVAl}

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      It should not allow different type of key to get added , if key type is defined while defining ConcurrentHashMap
      ACTUAL -
      Its lets any type be added.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      //Define ConcurrentHashMap for key type Integer
      ConcurrentHashMap<Integer, String> chm = new ConcurrentHashMap<Integer, String>();

      //Add Some thing
      chm.put(1, "Number");

      KeySetView kv = chm.keySet("SomeVAL");
      kv.add(true);
      kv.add("SomeString");
      System.out.println(chm);

      //OutPut
      //{1=Number, 2=String, 3=SomeVAl, 4=String, SomeString=SomeVAl, true=SomeVAl}
      ---------- END SOURCE ----------

        Attachments

          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: