Skip to content

Commit

Permalink
Polish "Avoid resizing of Maps created by CollectionUtils"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Sep 26, 2022
1 parent 874a296 commit 7309fe9
Showing 1 changed file with 6 additions and 4 deletions.
Expand Up @@ -85,8 +85,7 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) {
* @see #newLinkedHashMap(int)
*/
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
return new HashMap<>(capacity, DEFAULT_LOAD_FACTOR);
return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
}

/**
Expand All @@ -103,8 +102,11 @@ public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
* @see #newHashMap(int)
*/
public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int expectedSize) {
int capacity = (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
return new LinkedHashMap<>(capacity, DEFAULT_LOAD_FACTOR);
return new LinkedHashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
}

private static int computeMapInitialCapacity(int expectedSize) {
return (int) Math.ceil(expectedSize / (double) DEFAULT_LOAD_FACTOR);
}

/**
Expand Down

0 comments on commit 7309fe9

Please sign in to comment.