HashSet代码分析

 HashSet (jdk 1.7)的继承关系如下:

HashSet是使用HashMap实现的一个没有重复元素的集合。HashSet用法如下:

1
2
3
4
5
6
HashSet<String> hashSet = new HashSet<String>(); 
hashSet.add("java001"); 
hashSet.add("java01"); 
hashSet.add("java011"); 
hashSet.add("java002"); 
hashSet.add("java004"); 

从HashSet的add()方法可以看出,只有一个参数,并没有【键-值对】。

其实是HashSet只使用了HashMap的key,value统一是一个固定的Object,因此保证没有重复元素的方法,也是使用的HashMap的key来保证的。

 

既然HashSet是使用HashMap来实现的,那么HashSet必然会有一个HashMap的对象,即:

1
2
3
4
5
6
7
8
9
10
11
12
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<>();
}

其中,PRESENT = new Object()是用来作为HashMap的value使用的。可以通过其add()方法看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Adds the specified element to this set if it is not already present.
 * More formally, adds the specified element <tt>e</tt> to this set if
 * this set contains no element <tt>e2</tt> such that
 * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
 * If this set already contains the element, the call leaves the set
 * unchanged and returns <tt>false</tt>.
 *
 * @param e element to be added to this set
 * @return <tt>true</tt> if this set did not already contain the specified
 * element
 */
public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

在add()方法,添加的元素作为key,PRESENT作为value。

 

remove()方法也是调用的HashMap的remove()方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * Removes the specified element from this set if it is present.
 * More formally, removes an element <tt>e</tt> such that
 * <tt>(o==null ? e==null : o.equals(e))</tt>,
 * if this set contains such an element.  Returns <tt>true</tt> if
 * this set contained the element (or equivalently, if this set
 * changed as a result of the call).  (This set will not contain the
 * element once the call returns.)
 *
 * @param o object to be removed from this set, if present
 * @return <tt>true</tt> if the set contained the specified element
 */
public boolean remove(Object o) {
    return map.remove(o)==PRESENT;
}

  

同样,size() / isEmpty() / contains() / clear() 都是调用的HashMap相应的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Returns the number of elements in this set (its cardinality).
 *
 * @return the number of elements in this set (its cardinality)
 */
public int size() {
    return map.size();
}
 
/**
 * Returns <tt>true</tt> if this set contains no elements.
 *
 * @return <tt>true</tt> if this set contains no elements
 */
public boolean isEmpty() {
    return map.isEmpty();
}
/**
 * Returns <tt>true</tt> if this set contains the specified element.
 * More formally, returns <tt>true</tt> if and only if this set
 * contains an element <tt>e</tt> such that
 * <tt>(o==null ? e==null : o.equals(e))</tt>.
 *
 * @param o element whose presence in this set is to be tested
 * @return <tt>true</tt> if this set contains the specified element
 */
public boolean contains(Object o) {
    return map.containsKey(o);
}
 
/**
 * Removes all of the elements from this set.
 * The set will be empty after this call returns.
 */
public void clear() {
    map.clear();
}

  

另外,HashSet中有个更明显的方法来说明【HashSet只使用了HashMap的key】,即iterator()方法

1
2
3
4
5
6
7
8
9
10
/**
 * 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();
}

  

 

posted @   Entropy_lxl  阅读(193)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2015-04-12 Git学习笔记五--分支管理
2015-04-12 Git库搭建好之后,当要提交一个新的文件,需要做的是3个步骤
点击右上角即可分享
微信分享提示