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) 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));
}
}