HashSet底层简介提示:本文章参考jdk1.8
HashSet set = new HashSet<>();1、重要属性
private transient HashMap2、构造器map; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object();
public HashSet() {
//创建HashSet对象时,同时底层也创建了HashMap<>()对象
map = new HashMap<>();
}
3、add()方法
//HashSet的add()方法中储存数据相当于向HashMap的put()方法中储存数据
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
总结:HashSet底层实现的原理就是HashMap实现的原理


