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

Add a java.util.Properties constructor that takes an initial capacity

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 10
    • core-libs
    • None
    • source
    • minimal
    • The change adds a new public constructor. No changes are made to existing API.
    • Java API
    • SE

      Summary

      Add a new constructor to java.util.Properties that accepts an initial capacity.

      Problem

      There is currently no way to give a capacity hint to java.util.Properties. Properties' superclass, Hashtable, accepts such a hint, as do other commonly used Map implementations (HashMap, ConcurrentHashMap). One consequence is that extra time is spent at startup to expand Properties' storage while initializing system properties. Startup time could be reduced by being able to specify sufficient storage when a Properties is created.

      Solution

      A new constructor should be added to java.util.Properties that accepts an int argument to use as an initial capacity.

      Given this value will be used to configure the Properties' internal ConcurrentHashMap, it is convenient and sensible to interpret the value as ConcurrentHashMap does. Specifically, the value represents the number of elements that can be stored without expanding (rather than as the number of buckets, as in Hashtable).

      The new constructor matches the corresponding constructor in Properties' parent class, java.util.Hashtable. Hashtable also has constructors for specifying load factor. Although Properties inherits load factor from Hashtable (e.g. it's present in the serialized form), it it does not use the load factor. This fact is worth documenting.

      The other Properties constructors will be updated to call out that their initial capacity is unspecified.

      Specification

      +++ b/src/java.base/share/classes/java/util/Properties.java     Thu Oct 26 12:22:21 2017 -0700
      @@ -121,6 +121,10 @@
        * <p>This class is thread-safe: multiple threads can share a single
        * {@code Properties} object without the need for external synchronization.
        *
      + * @apiNote
      + * The {@code Properties} class does not inherit the concept of a load factor
      + * from its superclass, {@code Hashtable}.
      + * 
        * @author  Arthur van Hoff
      @@ -152,14 +156,35 @@
      
           /**
            * Creates an empty property list with no default values.
      +     * 
      +     * @implNote The initial capacity of a {@code Properties} object created
      +     * with this constructor is unspecified.
            */
           public Properties() {
               this(null);
           }
      
      +    /**
      +     * Creates an empty property list with no default values, and with an
      +     * initial size accommodating the specified number of elements without the
      +     * need to dynamically resize.
      +     *
      +     * @param  initialCapacity the {@code Properties} will be sized to
      +     *         accommodate this many elements 
      +     * @throws IllegalArgumentException if the initial capacity is less than
      +     *         zero.
      +     */
      +    public Properties(int initialCapacity);
      
           /**
            * Creates an empty property list with the specified defaults.
            *
      +     * @implNote The initial capacity of a {@code Properties} object created
      +     * with this constructor is unspecified.
      +     * 
            * @param   defaults   the defaults.
            */
           public Properties(Properties defaults);

            bchristi Brent Christian
            bchristi Brent Christian
            Alan Bateman, Brian Goetz, Mandy Chung (Inactive), Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: