Java 19 add newHashMap(int)
to HashMap
and LinkedHashMap
.
See: Java doc HashMap
private static final short DEFAULT_LOAD_FACTOR = .75;
public static int calculateMapCapacity(int numMappings) {
return (int) Math.ceil(numMappings / (double) DEFAULT_LOAD_FACTOR);
}
public static <K, V> HashMap<K, V> newHashMap(final int numMappings) {
return new HashMap<>(calculateMapCapacity(numMappings), DEFAULT_LOAD_FACTOR);
}