/**
*
* hashset的add方法
*
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
/**
*
* hashmap的put方法
*
*/
public V put(K key, V value) {
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
if (key == null)
return putForNullKey(value);
int hash = sun.misc.Hashing.singleWordWangJenkinsHash(key);
// 无序性 ? hash 和 table.length 计算出 index
// 所以在数组中的index 也是无序的 index并不是从0 。。。。 n 递增
// 所以遍历的时候 肯定是无序的
int i = indexFor(hash, table.length);
//table[i] 有HashMapEntry 了
for (HashMapEntry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
//如果key的hash 相等 并且 key相等 说明这个同一个元素 进入if判断
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
//仅仅是对 value 进行操作 并没有对key 进行操作
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
//返回了一个非null 对象
//hashset的add方法 返回false 证明在hashset中 增加失败
return oldValue;
}
}
//table[i] 没有HashMapEntry 证明key的hash 不冲突
//新增一个HashMapEntry后 整个函数 返回null
//hashset的add方法 返回true 证明在hashset中 增加成功
modCount++;
addEntry(hash, key, value, i);
return null;
}