doc: create Java HashSet section

This commit is contained in:
Leon Xu committed 2022-04-04 15:14:26 -07:00
1 parent bfe8efb696
commit 32887450b0
1 file changed
+15
+15
View File
@@ -188,3 +188,18 @@ HashMap的key是独一的的
2. 开放地址法 open addressing
好处:查找比纯链表快,插入删除比纯数组快;
## Set (Java HashSet)
特点:
1. 不允许出现重复元素
2. 不保证和集合中元素的顺序
3. 允许包含值为null的元素,但最多只能有一个null元素
它是基于HashMap实现的,HashSet底层使用HashMap来保存所有元素,因此HashSet 的实现比较简单,相关HashSet的操作,基本上都是直接调用底层HashMap的相关方法来完成
![hashset](/images/hashset-hashmap.png)
初始化Set<Integer> set = new HashSet<>();
常用method: 均为O(1)时间复杂度
set.add(1);
set.add(2);
set.contains(1); //return true
set.contains(3); //return false