|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public static void main(String[] args) { int aHundredMillion = 10000000; Map<Integer, Integer> map = new HashMap<>(); long s1 = System.currentTimeMillis(); for (int i = 0; i < aHundredMillion; i++) { map.put(i, i); } long s2 = System.currentTimeMillis(); System.out.println("未初始化容量,耗时 : "+ (s2 - s1)); Map<Integer, Integer> map1 = new HashMap<>(aHundredMillion / 2); long s5 = System.currentTimeMillis(); for (int i = 0; i < aHundredMillion; i++) { map1.put(i, i); } long s6 = System.currentTimeMillis(); System.out.println("初始化容量5000000,耗时 : "+ (s6 - s5)); Map<Integer, Integer> map2 = new HashMap<>(aHundredMillion); long s3 = System.currentTimeMillis(); for (int i = 0; i < aHundredMillion; i++) { map2.put(i, i); } long s4 = System.currentTimeMillis(); System.out.println("初始化容量为10000000,耗时 : "+ (s4 - s3));} |
|
1
2
3
|
未初始化容量,耗时 :14419初始化容量5000000,耗时 :11916初始化容量为10000000,耗时 :7984 |
|
1
2
3
4
5
6
7
|
Map<String, String> map = new HashMap<String, String>(1);map.put("hahaha", "hollischuang");Class<?> mapType = map.getClass();Method capacity = mapType.getDeclaredMethod("capacity");capacity.setAccessible(true);System.out.println("capacity : "+ capacity.invoke(map)); |
|
1
2
3
4
5
6
7
|
int n = cap - 1;n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16;return (n < 0) ?1 :(n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
|
1
2
3
4
5
|
n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16; |
|
1
|
return (n < 0) ?1 :(n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
|
1
2
3
4
5
6
|
1100 1100 1100 >>>1 = 0110 0110 01101100 1100 1100 | 0110 0110 0110 = 1110 1110 11101110 1110 1110 >>>2 = 0011 1011 10111110 1110 1110 | 0011 1011 1011 = 1111 1111 11111111 1111 1111 >>>4 = 1111 1111 11111111 1111 1111 | 1111 1111 1111 = 1111 1111 1111 |
|
1
2
3
4
5
6
7
|
Step 1: 0100 >>>1 = 00100100 | 0010 = 01100110 >>>1 = 00110110 | 0011 = 0111Step 2:0111 + 0001 = 1000 |
|
1
|
int n = cap - 1; |
|
1
2
3
4
5
6
7
|
int n = cap - 1;n |= n >>> 1;n |= n >>> 2;n |= n >>> 4;n |= n >>> 8;n |= n >>> 16;return (n < 0) ?1 :(n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) { return new HashMap<K, V>(capacity(expectedSize));}/*** Returns a capacity that is sufficient to keep the map from being resized as long as it grows no* larger than expectedSize and the load factor is ≥ its default (0.75).*/static int capacity(int expectedSize) { if (expectedSize < 3) { checkNonnegative(expectedSize, "expectedSize"); return expectedSize + 1; } if (expectedSize < Ints.MAX_POWER_OF_TWO) { // This is the calculation used in JDK8 to resize when a putAll // happens; it seems to be the most conservative calculation we // can make. 0.75 is the default load factor. return (int) ((float) expectedSize / 0.75F + 1.0F); } return Integer.MAX_VALUE; // any large value} |
原文:https://www.cnblogs.com/zhuxiaopijingjing/p/12258658.html