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

HashMap is calculating the threshold in a inconsistent way with previous version

XMLWordPrintable

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 8.1 Single Language x64 [versão 6.3.9600]

      A DESCRIPTION OF THE PROBLEM :
      Java 8's HashMap is calculating the new threshold and the new capacity inconsistently with previous versions.

      Look at this example:
      // -------------- Java 8 --------------
      Java8HashMap<String, Integer> map8 = new Java8HashMap<>(1, 0.33F);
      for (int i = 0; i < 81; i++) {
      map8.put("" + i, i);
      }
      System.out.println("Java 8 - Capacity: " + map8.table.length);
      System.out.println("Java 8 - Threshold: " + map8.threshold);

      // -------------- Java 7 --------------
      Java6HashMap<String, Integer> map7 = new Java6HashMap<>(1, 0.33F);
      for (int i = 0; i < 81; i++) {
      map7.put("" + i, i);
      }
      System.out.println("Java 7 - Capacity: " + map7.table.length);
      System.out.println("Java 7 - Threshold: " + map7.threshold);

      // -------------- Java 6 --------------
      Java6HashMap<String, Integer> map6 = new Java6HashMap<>(1, 0.33F);
      for (int i = 0; i < 81; i++) {
      map6.put("" + i, i);
      }
      System.out.println("Java 6 - Capacity: " + map6.table.length);
      System.out.println("Java 6 - Threshold: " + map6.threshold);

      Java8HashMap is the implementation of HashMap used by OpenJDK 8-b132
      Java7HashMap is the implementation of HashMap used by OpenJDK 7-b147
      Java6HashMap is the implementation of HashMap used by OpenJDK 6-b27

      Running the above example:
      For Java 8 - capacity: 512
                   threshold: 160
                   resize() method is called 9 times
      For Java 7 - capacity: 256
                   threshold: 84
                   resize() method is called 8 times
      For Java 6 - capacity: 256
                   threshold: 84
                   resize() method is called 8 times


      Not common values for the load factor causes unexpected results, look at more values:
      --------------------------------------------------------------------------
      initial capacity = 1
      load factor = 0.12F
      after inserting 81 different items, i get the below results:
      Java 8 - capacity: 1024
               threshold: 2147483647
      Java 7 - capacity: 1024
               threshold: 122
      Java 6 - capacity: 1024
               threshold: 122
      --------------------------------------------------------------------------
      initial capacity = 4
      load factor = 0.33F
      after inserting 21 different items, i get the below results:
      Java 8 - capacity: 128
               threshold: 40
      Java 7 - capacity: 64
               threshold: 21
      Java 6 - capacity: 64
               threshold: 21
      --------------------------------------------------------------------------
      initial capacity = 8
      load factor = 0.6F
      after inserting 21 different items, i get the below results:
      Java 8 - capacity: 64
               threshold: 36
      Java 7 - capacity: 64
               threshold: 38
      Java 6 - capacity: 64
               threshold: 38
      --------------------------------------------------------------------------
      But for the default load factor, the results for Java 8, 7, 6 are the same and expected.

      REGRESSION. Last worked in version 7u71

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Is in the description.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Is in the description.
      ACTUAL -
      Is in the description.

      REPRODUCIBILITY :
      This bug can be reproduced often.

      ---------- BEGIN SOURCE ----------
      Is in the description.
      ---------- END SOURCE ----------

            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: