Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8186958 Need method to create pre-sized HashMap
  3. JDK-8284975

Release Note: New Methods to Create Preallocated HashMaps and HashSets

    XMLWordPrintable

    Details

      Description

      New static factory methods have been introduced to allow creation of `HashMap` and related instances that are preallocated to accommodate an expected number of mappings or elements. After using the `HashMap.newHashMap` method, the requested number of mappings can be added to the newly created `HashMap` without it being resized. There are similar new static factory methods for `LinkedHashMap`, `WeakHashMap`, `HashSet`, and `LinkedHashSet`. The complete set of new methods is:

       * HashMap.newHashMap
       * LinkedHashMap.newLinkedHashMap
       * WeakHashMap.newWeakHashMap
       * HashSet.newHashSet
       * LinkedHashSet.newLinkedHashSet

      The int-argument constructors for these classes set the "capacity" (internal table size) which is not the same as the number of elements that can be accommodated. The capacity is related to the number of elements by a simple but error-prone calculation. For this reason, programs should use these new static factory methods in preference to the int-argument constructors.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              smarks Stuart Marks
              Reporter:
              smarks Stuart Marks
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: