HashSet和HashMap的关系
来源:互联网 发布:升级下windows updata 编辑:程序博客网 时间:2024/04/26 06:31
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; // Dummy value to associate with an Object in the backing Map private static final Object PRESENT = new Object(); /** * 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<E,Object>(); }......... /** * Returns an iterator over the elements in this set. The elements * are returned in no particular order. * * @return an Iterator over the elements in this set * @see ConcurrentModificationException */ public Iterator<E> iterator() {return map.keySet().iterator(); }
我貌似也遇到过这样的面试官 ,问这两者的关系,原来是HashSet就是用的HashMap啊,只是省略了value,只用了key