-
Enhancement
-
Resolution: Fixed
-
P4
-
None
It seems that people normally assume that the constructor HashMap(int) requires the expected size as the argument.
In fact, the argument is the initial capacity, which should be bigger than the expected size. As a result, unnecessary reallocations can occur while the maps are filled.
Retrospectively, is seems to be more convenient to have a constructor which specifies the expected size, like is was done with IdentityHashMap, for example.
Here is (presumably non complete) list of places under jdk/src/java.base where the initial capacity wasn't chosen optimal:
share/classes/sun/security/ssl/HandshakeStateManager.java: handshakeTypes = new HashMap<>(15);
share/classes/sun/security/provider/certpath/OCSPResponse.java: singleResponseMap = new HashMap<>(singleResponseDer.length);
share/classes/sun/security/provider/certpath/OCSPResponse.java: new HashMap<>(extDer.length);
share/classes/sun/security/util/Cache.java: Map<K,V> kvmap = new HashMap<>(cacheMap.size());
share/classes/sun/util/locale/provider/LocaleDataMetaInfo-XLocales.java.template: private static final Map<String, String> resourceNameToLocales = new HashMap<>(9);
share/classes/java/lang/Character.java: new HashMap<>((int)(510 / 0.75f + 1.0f));
share/classes/java/lang/Character.java: aliases = new HashMap<>(134);
share/classes/java/lang/Class.java: directory = new HashMap<>(2 * universe.length);
share/classes/java/lang/invoke/VarHandle.java: methodNameToAccessMode = new HashMap<>(AccessMode.values().length);
share/classes/java/lang/Thread.java: Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length);
share/classes/java/security/cert/PKIXRevocationChecker.java: Map<X509Certificate, byte[]> copy = new HashMap<>(responses.size());
share/classes/java/security/cert/PKIXRevocationChecker.java: Map<X509Certificate, byte[]> copy = new HashMap<>(ocspResponses.size());
share/classes/java/security/cert/CertificateRevokedException.java: extensions = new HashMap<>(size);
share/classes/java/text/NumberFormat.java: private static final Map<String, Field> instanceMap = new HashMap<>(11);
share/classes/java/text/AttributedCharacterIterator.java: private static final Map<String, Attribute> instanceMap = new HashMap<>(7);
share/classes/java/text/DateFormat.java: private static final Map<String, Field> instanceMap = new HashMap<>(18);
share/classes/java/util/jar/Attributes.java: map = new LinkedHashMap<>(size);
share/classes/java/util/jar/JarVerifier.java: signerMap = new HashMap<>(verifiedSigners.size() + sigFileSigners.size());
share/classes/java/util/ListResourceBundle.java: HashMap<String,Object> temp = new HashMap<>(contents.length);
share/classes/jdk/internal/module/ModuleInfo.java: Map<String, byte[]> map = new HashMap<>(hash_count);
share/classes/javax/net/ssl/SSLParameters.java: sniNames = new LinkedHashMap<>(serverNames.size());
share/classes/javax/net/ssl/SSLParameters.java: sniMatchers = new HashMap<>(matchers.size());
share/classes/com/sun/java/util/jar/pack/Package.java: allInnerClassesByThis = new HashMap<>(allInnerClasses.size());
unix/classes/sun/nio/fs/MimeTypesFileTypeDetector.java: mimeTypeMap = new HashMap<>(lines.size());
(smarks 2022-02-18)
In addition, see the cases covered by the tests in
- csr for
-
JDK-8284377 Need method to create pre-sized HashMap
- Closed
- relates to
-
JDK-8285386 java/util/HashMap/WhiteBoxResizeTest.java fails in tier7 after JDK-8186958
- Resolved
-
JDK-8282120 optimal capacity tests and test library need to be cleaned up
- Resolved
-
JDK-8281631 HashMap copy constructor and putAll can over-allocate table
- Closed
-
JDK-8200696 Optimal initial capacity of java.lang.Class.enumConstantDirectory
- Closed
-
JDK-8200788 Optimal initial capacity of java.lang.VarHandle.AccessMode.methodNameToAccessMode
- Closed
-
JDK-8285405 add test and check for negative argument to HashMap::newHashMap et al
- Resolved
-
JDK-8289872 wrong wording in @param doc for HashMap.newHashMap et. al.
- Resolved
-
JDK-8284780 Need methods to create pre-sized HashSet and LinkedHashSet
- Resolved