简介
HashSet的底层是由HashMap实现的,用了HashMap的key来存储元素,value为默认的对象,因HashMap的key是不能够重复的,所以也就造成了HashSet集合元素的不可重复性。
附HashSet源码如下:
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;
private transient HashMap<E,Object> map;
// 空对象,放在map中的value位置
private static final Object PRESENT = new Object();
/**
*构造一个新的,空的HashSet,其底层 HashMap实例的默认初始容量是 16,加载因子是 0.75
*/
public HashSet() {
map = new HashMap<>();
}
/**
* 构造一个包含指定 collection 中的元素的新 set,容量为传入集合长度除以默认加载因子0.75 与默认初始化容量16的最大值
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
/**
* 构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和指定的加载因子
*/
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
/**
*构造一个新的空 set,其底层 HashMap 实例具有指定的初始容量和默认的加载因子0.75
*/
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
/**
* Constructs a new, empty linked hash set. (This package private
* constructor is only used by LinkedHashSet.) The backing
* HashMap instance is a LinkedHashMap with the specified initial
* capacity and the specified load factor.
*
* @param initialCapacity the initial capacity of the hash map
* @param loadFactor the load factor of the hash map
* @param dummy ignored (distinguishes this
* constructor from other int, float constructor.)
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
/**
* 返回对此 set 中元素进行迭代的迭代器
*/
public Iterator<E> iterator() {
return map.keySet().iterator();
}
/**
* 获取数组个数,实际上是获取底层map中数据的个数
*/
public int size() {
return map.size();
}
/**
* 判断是否为空,实际上是判断map中的size是否等于0
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* 是都包含某元素,实际上是判断是否包含key,因为set只有单值,所map中其实只用到了key,map为空对象
*/
public boolean contains(Object o) {
return map.containsKey(o);
}
/**
*调用map的put方法,其中value值为静态的Object对象
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
/**
* 删除元素调用的是map的remove函数
*/
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
/**
* 清空集合,调用的是map的clear()
*/
public void clear() {
map.clear();
}
/**
* 返回此 HashSet 实例的浅表副本
*/
@SuppressWarnings("unchecked")
public Object clone() {
try {
HashSet<E> newSet = (HashSet<E>) super.clone();
newSet.map = (HashMap<E, Object>) map.clone();
return newSet;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
}