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

HashMap.putIfAbsent() puts elements in random order

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
      java version "1.8.0_151"
      Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

      A DESCRIPTION OF THE PROBLEM :
      HashMap.putIfAbsent puts elements in undetermined order when executing the following program multiple times:

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile the sample program
      2. Run it using Oracle HotSpot java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Elements always inserted in the same position.
      ACTUAL -
      Sometimes HashMap{Object=Object, Object=null}, sometimes HashMap{Object=null, Object=Object}.

      ---------- BEGIN SOURCE ----------
      public class A {
         public static void main(String args[]) {
      HashMap hm = new HashMap();
      hm.putIfAbsent(new Object(), new Object());
      hm.putIfAbsent(new Object(), null);
      System.out.println(hm.toString());
         }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: