oxsean commented on code in PR #13607:
URL: https://github.com/apache/dubbo/pull/13607#discussion_r1457011747
##########
dubbo-common/src/main/java/org/apache/dubbo/common/utils/CollectionUtils.java:
##########
@@ -420,4 +427,33 @@ public static <T> Set<T> toTreeSet(Set<T> set) {
}
return set;
}
+
+ public static <T> Set<T> newHashSet(int expectedSize) {
+ return new HashSet<>(capacity(expectedSize));
+ }
+
+ public static <T> Set<T> newLinkedHashSet(int expectedSize) {
+ return new LinkedHashSet<>(capacity(expectedSize));
+ }
+
+ public static <T, K> Map<K, T> newHashMap(int expectedSize) {
+ return new HashMap<>(capacity(expectedSize));
+ }
+
+ public static <T, K> Map<K, T> newLinkedHashMap(int expectedSize) {
+ return new LinkedHashMap<>(capacity(expectedSize));
+ }
+
+ public static int capacity(int expectedSize) {
+ if (expectedSize < 3) {
+ if (expectedSize < 0) {
+ throw new IllegalArgumentException("expectedSize cannot be
negative but was: " + expectedSize);
+ }
+ return expectedSize + 1;
+ }
+ if (expectedSize < 1 << (Integer.SIZE - 2)) {
+ return (int) (expectedSize / 0.75F + 1.0F);
+ }
+ return Integer.MAX_VALUE;
+ }
Review Comment:
Copy from guava,
https://stackoverflow.com/questions/30220820/difference-between-new-hashmapint-and-guava-maps-newhashmapwithexpectedsizein
Will add some comments.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]