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
HashSet和HashMap的关系
最新推荐文章于 2023-12-25 09:15:00 发布
本文深入探讨了Java中HashSet类如何利用HashMap实现高效性,详细解释了两者之间的区别与联系,帮助开发者更好地理解和使用集合框架。
3397

被折叠的 条评论
为什么被折叠?



