HashSet 底层是通过 HashMap 实现的。
HashMap 的键不能重复,HashSet 只取用了 HashMap 的键,
用 HashMap 的 键存储 HashSet 的值。
妙!
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}