8186958: Need method to create pre-sized HashMap

Reviewed-by: chegar, naoto, joehw, lancea, wetmore, smarks
This commit is contained in:
XenoAmess 2022-04-19 00:03:56 +00:00 committed by Stuart Marks
parent 41fc078323
commit 87faa85c59
30 changed files with 208 additions and 73 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -570,7 +570,7 @@ public class HostLocaleProviderAdapterImpl {
String[] names = getCalendarDisplayStrings(locale.toLanguageTag(),
field, style);
if (names != null) {
map = new HashMap<>((int)Math.ceil(names.length / 0.75));
map = HashMap.newHashMap(names.length);
for (int value = 0; value < names.length; value++) {
if (names[value] != null) {
map.put(names[value], value);

View File

@ -744,8 +744,7 @@ class Character implements java.io.Serializable, Comparable<Character>, Constabl
* 0.75 - the default load factor of HashMap
*/
private static final int NUM_ENTITIES = 737;
private static Map<String, UnicodeBlock> map =
new HashMap<>((int)(NUM_ENTITIES / 0.75f + 1.0f));
private static Map<String, UnicodeBlock> map = HashMap.newHashMap(NUM_ENTITIES);
/**
* Creates a UnicodeBlock with the given identifier name.
@ -8572,7 +8571,7 @@ class Character implements java.io.Serializable, Comparable<Character>, Constabl
private static final HashMap<String, Character.UnicodeScript> aliases;
static {
aliases = new HashMap<>((int)(162 / 0.75f + 1.0f));
aliases = HashMap.newHashMap(162);
aliases.put("ADLM", ADLAM);
aliases.put("AGHB", CAUCASIAN_ALBANIAN);
aliases.put("AHOM", AHOM);

View File

@ -3910,7 +3910,7 @@ public final class Class<T> implements java.io.Serializable,
if (universe == null)
throw new IllegalArgumentException(
getName() + " is not an enum class");
directory = new HashMap<>((int)(universe.length / 0.75f) + 1);
directory = HashMap.newHashMap(universe.length);
for (T constant : universe) {
directory.put(((Enum<?>)constant).name(), constant);
}
@ -4125,10 +4125,10 @@ public final class Class<T> implements java.io.Serializable,
Class<? extends Annotation> annotationClass = e.getKey();
if (AnnotationType.getInstance(annotationClass).isInherited()) {
if (annotations == null) { // lazy construction
annotations = new LinkedHashMap<>((Math.max(
annotations = LinkedHashMap.newLinkedHashMap(Math.max(
declaredAnnotations.size(),
Math.min(12, declaredAnnotations.size() + superAnnotations.size())
) * 4 + 2) / 3
)
);
}
annotations.put(annotationClass, e.getValue());

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -980,7 +980,7 @@ public final class Module implements AnnotatedElement {
// the packages to all unnamed modules.
Map<String, Set<Module>> openPackages = this.openPackages;
if (openPackages == null) {
openPackages = new HashMap<>((4 * (concealedPkgs.size() + exportedPkgs.size()) / 3) + 1);
openPackages = HashMap.newHashMap(concealedPkgs.size() + exportedPkgs.size());
} else {
openPackages = new HashMap<>(openPackages);
}
@ -1133,8 +1133,7 @@ public final class Module implements AnnotatedElement {
boolean isBootLayer = (ModuleLayer.boot() == null);
int numModules = cf.modules().size();
int cap = (int)(numModules / 0.75f + 1.0f);
Map<String, Module> nameToModule = new HashMap<>(cap);
Map<String, Module> nameToModule = HashMap.newHashMap(numModules);
// to avoid repeated lookups and reduce iteration overhead, we create
// arrays holding correlated information about each module.

View File

@ -1655,7 +1655,7 @@ public class Thread implements Runnable {
// Get a snapshot of the list of all threads
Thread[] threads = getThreads();
StackTraceElement[][] traces = dumpThreads(threads);
Map<Thread, StackTraceElement[]> m = new HashMap<>(threads.length);
Map<Thread, StackTraceElement[]> m = HashMap.newHashMap(threads.length);
for (int i = 0; i < threads.length; i++) {
StackTraceElement[] stackTrace = traces[i];
if (stackTrace != null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -290,7 +290,7 @@ abstract class MethodHandleImpl {
BoundMethodHandle mh = target.rebind();
// Match each unique conversion to the positions at which it is to be applied
var convSpecMap = new HashMap<Object, int[]>(((4 * convCount) / 3) + 1);
HashMap<Object, int[]> convSpecMap = HashMap.newHashMap(convCount);
for (int i = 0; i < convSpecs.length - MH_RECEIVER_OFFSET; i++) {
Object convSpec = convSpecs[i];
if (convSpec == null) continue;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -498,12 +498,11 @@ final class Resolver {
*/
private Map<ResolvedModule, Set<ResolvedModule>> makeGraph(Configuration cf) {
// initial capacity of maps to avoid resizing
int capacity = 1 + (4 * nameToReference.size())/ 3;
int moduleCount = nameToReference.size();
// the "reads" graph starts as a module dependence graph and
// is iteratively updated to be the readability graph
Map<ResolvedModule, Set<ResolvedModule>> g1 = new HashMap<>(capacity);
Map<ResolvedModule, Set<ResolvedModule>> g1 = HashMap.newHashMap(moduleCount);
// the "requires transitive" graph, contains requires transitive edges only
Map<ResolvedModule, Set<ResolvedModule>> g2;
@ -512,7 +511,7 @@ final class Resolver {
// as there may be selected modules that have a dependency on modules in
// the parent configuration.
if (ModuleLayer.boot() == null) {
g2 = new HashMap<>(capacity);
g2 = HashMap.newHashMap(moduleCount);
} else {
g2 = parents.stream()
.flatMap(Configuration::configurations)
@ -539,7 +538,7 @@ final class Resolver {
// populate g1 and g2 with the dependences from the selected modules
Map<String, ResolvedModule> nameToResolved = new HashMap<>(capacity);
Map<String, ResolvedModule> nameToResolved = HashMap.newHashMap(moduleCount);
for (ModuleReference mref : nameToReference.values()) {
ModuleDescriptor descriptor = mref.descriptor();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -245,7 +245,7 @@ public class CertificateRevokedException extends CertificateException {
} else if (size < 0) {
throw new IOException("size cannot be negative");
} else {
extensions = new HashMap<>(size > 20 ? 20 : size);
extensions = HashMap.newHashMap(size > 20 ? 20 : size);
}
// Read in the extensions and put the mappings in the extensions map

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -197,7 +197,7 @@ public abstract class PKIXRevocationChecker extends PKIXCertPathChecker {
if (responses == null) {
this.ocspResponses = Collections.<X509Certificate, byte[]>emptyMap();
} else {
Map<X509Certificate, byte[]> copy = new HashMap<>(responses.size());
Map<X509Certificate, byte[]> copy = HashMap.newHashMap(responses.size());
for (Map.Entry<X509Certificate, byte[]> e : responses.entrySet()) {
copy.put(e.getKey(), e.getValue().clone());
}
@ -216,7 +216,7 @@ public abstract class PKIXRevocationChecker extends PKIXCertPathChecker {
* Returns an empty map if no responses have been specified.
*/
public Map<X509Certificate, byte[]> getOcspResponses() {
Map<X509Certificate, byte[]> copy = new HashMap<>(ocspResponses.size());
Map<X509Certificate, byte[]> copy = HashMap.newHashMap(ocspResponses.size());
for (Map.Entry<X509Certificate, byte[]> e : ocspResponses.entrySet()) {
copy.put(e.getKey(), e.getValue().clone());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -433,6 +433,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
* Constructs an empty {@code HashMap} with the specified initial
* capacity and load factor.
*
* @apiNote
* To create a {@code HashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newHashMap(int) newHashMap}.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
@ -455,6 +459,10 @@ public class HashMap<K,V> extends AbstractMap<K,V>
* Constructs an empty {@code HashMap} with the specified initial
* capacity and the default load factor (0.75).
*
* @apiNote
* To create a {@code HashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newHashMap(int) newHashMap}.
*
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
@ -2545,4 +2553,32 @@ public class HashMap<K,V> extends AbstractMap<K,V>
}
}
/**
* Calculate initial capacity for HashMap based classes, from expected size and default load factor (0.75).
*
* @param numMappings the expected number of mappings
* @return initial capacity for HashMap based classes.
* @since 19
*/
static int calculateHashMapCapacity(int numMappings) {
return (int) Math.ceil(numMappings / (double) DEFAULT_LOAD_FACTOR);
}
/**
* Creates a new, empty HashMap suitable for the expected number of mappings.
* The returned map uses the default load factor of 0.75, and its initial capacity is
* generally large enough so that the expected number of mappings can be added
* without resizing the map.
*
* @param numMappings the expected number of mappings
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @return the newly created map
* @throws IllegalArgumentException if numMappings is negative
* @since 19
*/
public static <K, V> HashMap<K, V> newHashMap(int numMappings) {
return new HashMap<>(calculateHashMapCapacity(numMappings));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -117,7 +117,7 @@ public class HashSet<E>
* @throws NullPointerException if the specified collection is null
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
map = HashMap.newHashMap(Math.max(c.size(), 12));
addAll(c);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -339,6 +339,10 @@ public class LinkedHashMap<K,V>
* Constructs an empty insertion-ordered {@code LinkedHashMap} instance
* with the specified initial capacity and load factor.
*
* @apiNote
* To create a {@code LinkedHashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newLinkedHashMap(int) newLinkedHashMap}.
*
* @param initialCapacity the initial capacity
* @param loadFactor the load factor
* @throws IllegalArgumentException if the initial capacity is negative
@ -353,6 +357,10 @@ public class LinkedHashMap<K,V>
* Constructs an empty insertion-ordered {@code LinkedHashMap} instance
* with the specified initial capacity and a default load factor (0.75).
*
* @apiNote
* To create a {@code LinkedHashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newLinkedHashMap(int) newLinkedHashMap}.
*
* @param initialCapacity the initial capacity
* @throws IllegalArgumentException if the initial capacity is negative
*/
@ -788,5 +796,21 @@ public class LinkedHashMap<K,V>
public final Map.Entry<K,V> next() { return nextNode(); }
}
/**
* Creates a new, empty, insertion-ordered LinkedHashMap suitable for the expected number of mappings.
* The returned map uses the default load factor of 0.75, and its initial capacity is
* generally large enough so that the expected number of mappings can be added
* without resizing the map.
*
* @param numMappings the expected number of mappings
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @return the newly created map
* @throws IllegalArgumentException if numMappings is negative
* @since 19
*/
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int numMappings) {
return new LinkedHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -193,7 +193,7 @@ public abstract class ListResourceBundle extends ResourceBundle {
return;
Object[][] contents = getContents();
HashMap<String,Object> temp = new HashMap<>(contents.length);
HashMap<String,Object> temp = HashMap.newHashMap(contents.length);
for (Object[] content : contents) {
// key must be non-null String, value must be non-null
String key = (String) content[0];

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -198,6 +198,10 @@ public class WeakHashMap<K,V>
* Constructs a new, empty {@code WeakHashMap} with the given initial
* capacity and the given load factor.
*
* @apiNote
* To create a {@code WeakHashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newWeakHashMap(int) newWeakHashMap}.
*
* @param initialCapacity The initial capacity of the {@code WeakHashMap}
* @param loadFactor The load factor of the {@code WeakHashMap}
* @throws IllegalArgumentException if the initial capacity is negative,
@ -223,6 +227,10 @@ public class WeakHashMap<K,V>
* Constructs a new, empty {@code WeakHashMap} with the given initial
* capacity and the default load factor (0.75).
*
* @apiNote
* To create a {@code WeakHashMap} with an initial capacity that accommodates
* an expected number of mappings, use {@link #newWeakHashMap(int) newWeakHashMap}.
*
* @param initialCapacity The initial capacity of the {@code WeakHashMap}
* @throws IllegalArgumentException if the initial capacity is negative
*/
@ -1335,4 +1343,21 @@ public class WeakHashMap<K,V>
}
}
/**
* Creates a new, empty WeakHashMap suitable for the expected number of mappings.
* The returned map uses the default load factor of 0.75, and its initial capacity is
* generally large enough so that the expected number of mappings can be added
* without resizing the map.
*
* @param numMappings the expected number of mappings
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
* @return the newly created map
* @throws IllegalArgumentException if numMappings is negative
* @since 19
*/
public static <K, V> WeakHashMap<K, V> newWeakHashMap(int numMappings) {
return new WeakHashMap<>(HashMap.calculateHashMapCapacity(numMappings));
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -69,7 +69,7 @@ public class Attributes implements Map<Object,Object>, Cloneable {
* Constructs a new, empty Attributes object with default size.
*/
public Attributes() {
this(11);
this(16);
}
/**
@ -79,7 +79,7 @@ public class Attributes implements Map<Object,Object>, Cloneable {
* @param size the initial number of attributes
*/
public Attributes(int size) {
map = new LinkedHashMap<>(size);
map = LinkedHashMap.newLinkedHashMap(size);
}
/**

View File

@ -672,7 +672,7 @@ class JarVerifier {
* only about the asserted signatures. Verification of
* signature validity happens via the JarEntry apis.
*/
signerMap = new HashMap<>(verifiedSigners.size() + sigFileSigners.size());
signerMap = HashMap.newHashMap(verifiedSigners.size() + sigFileSigners.size());
signerMap.putAll(verifiedSigners);
signerMap.putAll(sigFileSigners);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -588,7 +588,7 @@ public final class ModuleInfo {
String algorithm = cpool.getUtf8(algorithm_index);
int hash_count = in.readUnsignedShort();
Map<String, byte[]> map = new HashMap<>(hash_count);
Map<String, byte[]> map = HashMap.newHashMap(hash_count);
for (int i=0; i<hash_count; i++) {
int module_name_index = in.readUnsignedShort();
String mn = cpool.getModuleName(module_name_index);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -307,7 +307,7 @@ public final class OCSPResponse {
// responses
DerValue[] singleResponseDer = seqDerIn.getSequence(1);
singleResponseMap = new HashMap<>(singleResponseDer.length);
singleResponseMap = HashMap.newHashMap(singleResponseDer.length);
if (debug != null) {
debug.println("OCSP number of SingleResponses: "
+ singleResponseDer.length);
@ -751,7 +751,7 @@ public final class OCSPResponse {
parseExtensions(DerValue derVal) throws IOException {
DerValue[] extDer = derVal.data.getSequence(3);
Map<String, java.security.cert.Extension> extMap =
new HashMap<>(extDer.length);
HashMap.newHashMap(extDer.length);
for (DerValue extDerVal : extDer) {
Extension ext = new Extension(extDerVal);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -481,7 +481,7 @@ class MemoryCache<K,V> extends Cache<K,V> {
}
private Map<K,V> getCachedEntries() {
Map<K,V> kvmap = new HashMap<>(cacheMap.size());
Map<K,V> kvmap = HashMap.newHashMap(cacheMap.size());
for (CacheEntry<K,V> entry : cacheMap.values()) {
kvmap.put(entry.getKey(), entry.getValue());

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -157,7 +157,7 @@ public abstract class OpenListResourceBundle extends ResourceBundle {
* Default uses HashMap.
*/
protected <K, V> Map<K, V> createMap(int size) {
return new HashMap<>(size);
return HashMap.newHashMap(size);
}
protected <E> Set<E> createSet() {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -96,7 +96,7 @@ public abstract class TimeZoneNamesBundle extends OpenListResourceBundle {
*/
@Override
protected <K, V> Map<K, V> createMap(int size) {
return new LinkedHashMap<>(size);
return LinkedHashMap.newLinkedHashMap(size);
}
/**

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -107,7 +107,7 @@ class MimeTypesFileTypeDetector extends AbstractFileTypeDetector {
}
});
mimeTypeMap = new HashMap<>(lines.size());
mimeTypeMap = HashMap.newHashMap(lines.size());
String entry = "";
for (String line : lines) {
entry += line;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -108,7 +108,7 @@ final class HeaderTable extends SimpleHeaderTable {
static {
Map<String, Map<String, Integer>> map
= new HashMap<>(STATIC_TABLE_LENGTH);
= HashMap.newHashMap(STATIC_TABLE_LENGTH);
for (int i = 1; i <= STATIC_TABLE_LENGTH; i++) {
HeaderField f = staticTable.get(i);
Map<String, Integer> values
@ -116,7 +116,7 @@ final class HeaderTable extends SimpleHeaderTable {
values.put(f.value, i);
}
// create an immutable deep copy
Map<String, Map<String, Integer>> copy = new HashMap<>(map.size());
Map<String, Map<String, Integer>> copy = HashMap.newHashMap(map.size());
for (Map.Entry<String, Map<String, Integer>> e : map.entrySet()) {
copy.put(e.getKey(), Map.copyOf(e.getValue()));
}

View File

@ -107,7 +107,7 @@ public final class DOMXPathFilter2Transform extends ApacheTransform {
if (attributes != null) {
int length = attributes.getLength();
Map<String, String> namespaceMap =
new HashMap<>(length);
new HashMap<>((int) Math.ceil(length / 0.75));
for (int i = 0; i < length; i++) {
Attr attr = (Attr)attributes.item(i);
String prefix = attr.getPrefix();

View File

@ -75,7 +75,7 @@ public final class DOMXPathTransform extends ApacheTransform {
if (attributes != null) {
int length = attributes.getLength();
Map<String, String> namespaceMap =
new HashMap<>(length);
new HashMap<>((int) Math.ceil(length / 0.75));
for (int i = 0; i < length; i++) {
Attr attr = (Attr)attributes.item(i);
String prefix = attr.getPrefix();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -50,6 +50,7 @@ import org.xml.sax.XMLReader;
/**
* @author Morten Jorgensen
* @LastModified: Apr 2022
*/
public final class DocumentCache implements DOMCache {
@ -168,7 +169,7 @@ public final class DocumentCache implements DOMCache {
_count = 0;
_current = 0;
_size = size;
_references = new HashMap<>(_size+2);
_references = HashMap.newHashMap(_size);
_URIs = new String[_size];
try {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -83,7 +83,7 @@ import org.w3c.dom.ls.LSSerializer;
* @author Andy Clark, IBM
* @author Ralf Pfeiffer, IBM
* @since PR-DOM-Level-1-19980818.
* @LastModified: Sept 2019
* @LastModified: Apr 2022
*/
public class CoreDocumentImpl
extends ParentNode implements Document {
@ -393,7 +393,7 @@ public class CoreDocumentImpl
if (identifiers != null) {
// Build a reverse mapping from element to identifier.
reversedIdentifiers = new HashMap<>(identifiers.size());
reversedIdentifiers = HashMap.newHashMap(identifiers.size());
for (String elementId : identifiers.keySet()) {
reversedIdentifiers.put(identifiers.get(elementId), elementId);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -67,7 +67,7 @@ import org.w3c.dom.Element;
* @xerces.internal
*
* @author Sandy Gao, IBM
* @LastModified: Nov 2017
* @LastModified: Apr 2022
*/
public class XSAttributeChecker {
@ -1816,7 +1816,7 @@ class SmallContainer extends Container {
class LargeContainer extends Container {
Map<String, OneAttr> items;
LargeContainer(int size) {
items = new HashMap<>(size*2+1);
items = HashMap.newHashMap(size);
values = new OneAttr[size];
}
void put(String key, OneAttr value) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1561,7 +1561,7 @@ class ZipFileSystem extends FileSystem {
throw new ZipException("read CEN tables failed");
}
// Iterate through the entries in the central directory
inodes = new LinkedHashMap<>(end.centot + 1);
inodes = LinkedHashMap.newLinkedHashMap(end.centot + 1);
int pos = 0;
int limit = cen.length - ENDHDR;
while (pos < limit) {

View File

@ -48,7 +48,7 @@ import static org.testng.Assert.assertNull;
/*
* @test
* @bug 8210280 8281631
* @bug 8186958 8210280 8281631
* @modules java.base/java.util:open
* @summary White box tests for HashMap-related internals around table sizing
* @run testng WhiteBoxResizeTest
@ -201,7 +201,7 @@ public class WhiteBoxResizeTest {
@DataProvider(name = "requestedCapacity")
public Iterator<Object[]> requestedCapacityCases() {
ArrayList<Object[]> cases = new ArrayList<>();
for (int i = 2; i < 128; i++) {
for (int i = 2; i < 64; i++) {
int cap = i;
cases.add(new Object[]{"rhm1", cap, (Supplier<Map<String, String>>) () -> new HashMap<>(cap)});
cases.add(new Object[]{"rhm2", cap, (Supplier<Map<String, String>>) () -> new HashMap<>(cap, 0.75f)});
@ -292,16 +292,19 @@ public class WhiteBoxResizeTest {
@DataProvider(name = "populatedCapacity")
public Iterator<Object[]> populatedCapacityCases() {
ArrayList<Object[]> cases = new ArrayList<>();
cases.addAll(genPopulatedCapacityCases(11, 16));
cases.addAll(genPopulatedCapacityCases(12, 16));
cases.addAll(genPopulatedCapacityCases(13, 32));
cases.addAll(genPopulatedCapacityCases(64, 128));
cases.addAll(genPopulatedCapacityCases(24, 32));
cases.addAll(genPopulatedCapacityCases(25, 64));
cases.addAll(genPopulatedCapacityCases(48, 64));
cases.addAll(genPopulatedCapacityCases(49, 128));
// numbers in this range are truncated by a float computation with 0.75f
// but can get an exact result with a double computation with 0.75d
cases.addAll(genFakePopulatedCapacityCases(25165824, 33554432));
cases.addAll(genFakePopulatedCapacityCases(25165825, 67108864));
cases.addAll(genFakePopulatedCapacityCases(25165826, 67108864));
cases.addAll(genFakePopulatedCapacityCases(50331648, 67108864));
cases.addAll(genFakePopulatedCapacityCases(50331649, 134217728));
return cases.iterator();
}
@ -317,4 +320,53 @@ public class WhiteBoxResizeTest {
assertEquals(capacity(map), expectedCapacity);
}
/*
* tests for requested size (static factory methods)
*/
// helper method for one requested size case, to provide target types for lambda
Object[] rsc(String label,
int size,
int expectedCapacity,
Supplier<Map<String, String>> supplier) {
return new Object[]{label, size, expectedCapacity, supplier};
}
List<Object[]> genRequestedSizeCases(int size, int cap) {
return Arrays.asList(
rsc("rshm", size, cap, () -> HashMap.newHashMap(size)),
rsc("rslm", size, cap, () -> LinkedHashMap.newLinkedHashMap(size)),
rsc("rswm", size, cap, () -> WeakHashMap.newWeakHashMap(size))
);
}
@DataProvider(name = "requestedSize")
public Iterator<Object[]> requestedSizeCases() {
ArrayList<Object[]> cases = new ArrayList<>();
cases.addAll(genRequestedSizeCases(12, 16));
cases.addAll(genRequestedSizeCases(13, 32));
cases.addAll(genRequestedSizeCases(24, 32));
cases.addAll(genRequestedSizeCases(25, 64));
cases.addAll(genRequestedSizeCases(48, 64));
cases.addAll(genRequestedSizeCases(49, 128));
// numbers in this range are truncated by a float computation with 0.75f
// but can get an exact result with a double computation with 0.75d
cases.addAll(genRequestedSizeCases(25165824, 33554432));
cases.addAll(genRequestedSizeCases(25165825, 67108864));
cases.addAll(genRequestedSizeCases(50331648, 67108864));
cases.addAll(genRequestedSizeCases(50331649, 134217728));
return cases.iterator();
}
@Test(dataProvider = "requestedSize")
public void requestedSize(String label, // unused, included for diagnostics
int size, // unused, included for diagnostics
int expectedCapacity,
Supplier<Map<String, String>> s) {
Map<String, String> map = s.get();
map.put("", "");
assertEquals(capacity(map), expectedCapacity);
}
}