一、前言
在學習java源碼中,全是英文,看起來就有點費勁了,所以就把這些註釋翻譯了一遍。
採用機器翻譯,多少會有不準的地方。意思能看明白就可以了。
這是我自己學習Hashmap源碼的時候做的一件事。
壓縮包裏存放着的和原來的是一樣的:
二、效果
中英對照:
可以學英語,又可以學代碼,兩全其美
三、如何得到
(一)有力的可用此方式
有力的可以到這裏下載:https://download.csdn.net/download/qq_17623363/12578222
(二)無力的可用此方式
無力的可以加入此微信羣下載哦(完全免費提供給大家):
我已加入CSDN合夥人計劃
親愛的各位粉絲:可以添加我的CSDN官方企業微信號,和我近距離互動聊天,爲您答疑解惑
。
直接使用微信掃碼即可,不用下載企業微信
。
四、關鍵代碼實例
/**
* Constructs a new <tt>HashMap</tt> with the same mappings as the
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
* default load factor (0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
*
* 使用與指定Map相同的映射構造一個新的HashMap。
* 使用默認的負載因子(0.75)和足以將映射保存在指定Map中的初始容量創建HashMap。
*
* @param m the map whose mappings are to be placed in this map
* @throws NullPointerException if the specified map is null
*/
public HashMap(Map<? extends K, ? extends V> m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
inflateTable(threshold);
putAllForCreate(m);
}
/*
Integer.highestOneBit(15) = 8 :找到一個小於等於這個數的二的次方數
爲什麼先把傳入的值減一呢?
如果傳入的number是8,那麼直接返回的就是8了,如果減一的話,number就等於7
7: 0000 0111
7 << 1 = 14 = 0001 0100 翻了一倍,這樣的話,返回的結果就是翻倍後的這個數的最小的二次冪
演算:
0001 0100
0001 0100 >> 1 = 0000 1010 ;
0001 0100
| 0000 1010
= 0001 1110
0001 1110 >> 2 = 0000 0111
0001 1110
|0000 0111
=0001 1111
0001 1111 >>> 1 = 0000 1111
0001 1111
-0000 1111
0001 0000
最終結果:(2)0001 0000 = (10)16
*/
//向上舍入爲2的冪
private static int roundUpToPowerOf2(int number) {
// assert number >= 0 : "number must be non-negative";
return number >= MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (number > 1) ? Integer.highestOneBit((number - 1) << 1) : 1;
}
/**
* Inflates the table.
* 初始化table數組
*/
private void inflateTable(int toSize) {
// Find a power of 2 >= toSize 找一個 2 >= toSize的2的次方數
int capacity = roundUpToPowerOf2(toSize);
//擴容要用到的值
threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
//使用計算出來的值,初始化存儲數據的數組
table = new Entry[capacity];
initHashSeedAsNeeded(capacity);
}
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
* 將指定值與該映射中的指定鍵相關聯。
* 如果該映射先前包含該key的映射,則將替換舊值。
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
*/
public V put(K key, V value) {
//先判斷數組是不是一個空的,代表數組還未初始化;
//類似懶加載,或者說是初始化,只有在put存元素的時候,纔會真正的初始化裏面的數組
if (table == EMPTY_TABLE) {
inflateTable(threshold);//初始化的方法
}
if (key == null)
return putForNullKey(value);
int hash = hash(key);//計算一個哈希值
int i = indexFor(hash, table.length);//計算索引位置 索引始終保持在0-table.length之間
//for():爲了查找是不是重複的,如果key是重複的,就去覆蓋掉舊的key的值,把原來的key的值給返回
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
//如果沒有重複的就繼續執行
modCount++;
// key的hash值,key,val,索引
addEntry(hash, key, value, i);
return null;
}
五、如何使用
下載下來之後,需要先解壓出來:
創建一個普通的java項目,把下載的src中的文件解壓到這個項目中的src包下:
如果你是idea:
選擇jdk1.7版本:
只需要按下面配置即可:
這樣的話,就可以隨時進行編輯了,想怎麼註釋就可以怎麼註釋。