From 673ca7db212060496697daf8744bc45b953682be Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Mon, 4 Apr 2022 15:14:26 -0700 Subject: [PATCH] doc: create Java HashSet section --- source/_posts/数据结构.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/source/_posts/数据结构.md b/source/_posts/数据结构.md index 5b4f11c..abc1c61 100644 --- a/source/_posts/数据结构.md +++ b/source/_posts/数据结构.md @@ -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 set = new HashSet<>(); +常用method: 均为O(1)时间复杂度 +set.add(1); +set.add(2); +set.contains(1); //return true +set.contains(3); //return false \ No newline at end of file