Skip to content

Commit

Permalink
Merge pull request #29190 from dreis2211
Browse files Browse the repository at this point in the history
* pr/29190:
  Polish "Avoid resizing of Maps created by CollectionUtils"
  Avoid resizing of Maps created by CollectionUtils

Closes gh-29190
  • Loading branch information
snicoll committed Sep 26, 2022
2 parents d872e7c + 7309fe9 commit 433a23a
Showing 1 changed file with 7 additions and 3 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,7 +85,7 @@ public static boolean isEmpty(@Nullable Map<?, ?> map) {
* @see #newLinkedHashMap(int)
*/
public static <K, V> HashMap<K, V> newHashMap(int expectedSize) {
return new HashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), DEFAULT_LOAD_FACTOR);
return new HashMap<>(computeMapInitialCapacity(expectedSize), DEFAULT_LOAD_FACTOR);
}

/**
Expand All @@ -102,7 +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) {
return new LinkedHashMap<>((int) (expectedSize / DEFAULT_LOAD_FACTOR), 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 433a23a

Please sign in to comment.