关联项目需求
进行FeatureAB上报的时候,我们使用HashSet的add方法存key值,如果key已存在,则add失败,返回false,如果key不存在,add成功,返回true。
看源码中HashSet的add(E e)方法实现:
HashSet#add
底层还是使用的hashMap的put(E e,Object o)方法。
HashMap的结构:
使用数组的结构存链表(或树)的节点
transient Node<K,V>[] table;
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;
Node(int hash, K key, V value, Node<K,V> next) {
this.hash = hash;
this.key = key;
this.value = value;
this.next = next;
}
public final K getKey() { return key; }
public final V getValue() { return value; }
public final String toString() { return key + "=" + value; }
public final int hashCode() {
return Objects.hashCode(key) ^ Objects.hashCode(value);
}
...
}
hashMap结构:
HashMap的put方法
public V put(K key, V value) {
return putVal(hash(