注:转载请注明出处!!!!!!!这里咱们看的是JDK1.7版本的HashMap

学习HashMap前先知道熟悉运算符合

 *左移 << :就是该数对应二进制码整体左移,左边超出的部分舍弃,右边补零。举个例子:253的二进制码1111 1101,在经过运算253<<2后得到1111 0100。很简单  

*右移 >> :该数对应的二进制码整体右移,左边的用原有标志位补充,右边超出的部分舍弃。

*无符号右移 >>> :不管正负标志位为0还是1,将该数的二进制码整体右移,左边部分总是以0填充,右边部分舍弃。

*取模运算 h & (length-1) 就是 h%lenght ,但是&比%具有更高的效率

总结下 X>>Y  相当于X/(2^Y)  X<<Y 相当于X*(2^Y)

知道了运算后,先看创建HashMap的4个构造方法

 1 /**
 2      * Constructs an empty <tt>HashMap</tt> with the specified initial
 3      * capacity and load factor.
 4      *
 5      * @param  initialCapacity the initial capacity
 6      * @param  loadFactor      the load factor
 7      * @throws IllegalArgumentException if the initial capacity is negative
 8      *         or the load factor is nonpositive
 9      */
10 
11     //初始化HashMap,使用传入的初始容量和加载因子
12     public HashMap(int initialCapacity, float loadFactor) {
13         //初始容量不能小于0否则抛异常
14         if (initialCapacity < 0)
15             throw new IllegalArgumentException("Illegal initial capacity: " +
16                                                initialCapacity);
17         //如果自定义的初始容量大于默认的最大容量(1<<30=2^30)则将默认     
18         //最大容量赋值给传入的初始容量
19         if (initialCapacity > MAXIMUM_CAPACITY)
20             initialCapacity = MAXIMUM_CAPACITY;
21         //如果加载因子小于等于0或者加载因子不是一数字,抛异常
22         if (loadFactor <= 0 || Float.isNaN(loadFactor))
23             throw new IllegalArgumentException("Illegal load factor: " +
24                                                loadFactor);
25 
26         //当前加载因子=传入加载因子
27         this.loadFactor = loadFactor;
28         //扩容变量 = 传入初始容量
29         threshold = initialCapacity;
30         //初始化
31         init();
32     }
33 
34 /**
35      * Constructs an empty <tt>HashMap</tt> with the specified initial
36      * capacity and the default load factor (0.75).
37      *
38      * @param  initialCapacity the initial capacity.
39      * @throws IllegalArgumentException if the initial capacity is negative.
40      */
41 
42     //初始化HashMap传入一个自定义的初始容量,默认的加载因子(0.75)
43     public HashMap(int initialCapacity) {
44         //走上面的初始化方法
45         this(initialCapacity, DEFAULT_LOAD_FACTOR);
46     }
47 
48     /**
49      * Constructs an empty <tt>HashMap</tt> with the default initial capacity
50      * (16) and the default load factor (0.75).
51      */
52      //初始化HashMap,使用默认的初始容量(1<<4= 2^4 =16)
53      //和默认的加载因子(0.75)
54     public HashMap() {
55        //同上
56        this(DEFAULT_INITIAL_CAPACITY,DEFAULT_LOAD_FACTOR);
57     }
58 
59  
60 
61     /**
62      * Constructs a new <tt>HashMap</tt> with the same mappings as the
63      * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
64      * default load factor (0.75) and an initial capacity sufficient to
65      * hold the mappings in the specified <tt>Map</tt>.
66      *
67      * @param   m the map whose mappings are to be placed in this map
68      * @throws  NullPointerException if the specified map is null
69      */
70     
71     //构造一个映射关系与指定 Map 相同的新 HashMap
72     public HashMap(Map<? extends K, ? extends V> m) {
73         //调用自己的构造,取出最大初始容量,默认加载因子
74         this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
75                       DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
76         //创建一个Entry[],初始化Hash掩饰码
77         inflateTable(threshold);
78         //将m的值put到新的HashMap中或者创建一个新的HashMap
79         putAllForCreate(m);
80     }
View Code

相关文章: