- 执行如下程序
HashSet hashSet = new HashSet();
hashSet.add("java");
hashSet.add("php");
hashSet.add("java");
System.out.println("hashset = " + hashSet);
1.执行HashSet()
public HashSet() {
map = new HashMap<>();
}
2. 执行add()
public boolean add(E e) { //e = "java
return map.put(e, PRESENT)==null; //private static final Object PRESENT = new Object();
}
3.执行put()
,该方法会执行hash(key)
得到key对应的hash值,
算法:key.hashCode() ^ (h >>> 16)
public V put(K key, V value) { //key = "java"; value = PRESENT(共享)
return putVal(hash(key), key, value, false, tr