js-apis-hashset.md 6.6 KB
Newer Older
L
linhaoran 已提交
1 2 3 4 5
# 非线性容器HashSet 

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

6 7 8 9 10
HashSet基于[HashMap](js-apis-hashmap.md)实现。在HashSet中,只对value对象进行处理。

HashSet和[TreeSet](js-apis-treeset.md)相比,HashSet中的数据无序存放,即存放元素的顺序和取出的顺序不一致,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不允许。

**推荐使用场景:** 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。
L
linhaoran 已提交
11 12 13

## 导入模块

14
```ts
15
import HashSet from '@ohos.util.HashSet';
L
linhaoran 已提交
16 17 18 19 20 21
```

## HashSet

### 属性

Z
zengyawen 已提交
22 23
**系统能力:** SystemCapability.Utils.Lang

L
linhaoran 已提交
24 25
| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
26
| length | number | 是 | 否 | HashSet的元素个数。 |
L
linhaoran 已提交
27 28 29 30


### constructor

Z
zengyawen 已提交
31
constructor()
L
linhaoran 已提交
32 33 34

HashSet的构造函数。

Z
zengyawen 已提交
35 36
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
37 38
**示例:**

39
```ts
Z
zengyawen 已提交
40 41
let hashSet = new HashSet();
```
L
linhaoran 已提交
42 43 44 45


### isEmpty

Z
zengyawen 已提交
46
isEmpty(): boolean
L
linhaoran 已提交
47 48 49

判断该HashSet是否为空。

Z
zengyawen 已提交
50 51
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
52
**返回值:**
L
linhaoran 已提交
53

Z
zengyawen 已提交
54 55 56 57 58 59
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |

**示例:**

60
```ts
Z
zengyawen 已提交
61
const hashSet = new HashSet();
62
let result = hashSet.isEmpty();
Z
zengyawen 已提交
63
```
L
linhaoran 已提交
64 65 66 67


### has

Z
zengyawen 已提交
68
has(value: T): boolean
L
linhaoran 已提交
69

W
wusongqing 已提交
70
判断此HashSet中是否含有该指定元素。
L
linhaoran 已提交
71

Z
zengyawen 已提交
72 73
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
74 75 76 77 78 79 80
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 指定元素。 |

**返回值:**
L
linhaoran 已提交
81

Z
zengyawen 已提交
82 83 84
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |
L
linhaoran 已提交
85

Z
zengyawen 已提交
86 87
**示例:**

88
```ts
Z
zengyawen 已提交
89
let hashSet = new HashSet();
90
let result = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
Z
zengyawen 已提交
91
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
92
let result1 = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
Z
zengyawen 已提交
93
```
L
linhaoran 已提交
94 95 96 97


### add

Z
zengyawen 已提交
98
add(value: T): boolean
L
linhaoran 已提交
99 100 101

向HashSet中添加数据。

Z
zengyawen 已提交
102 103
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
104 105 106 107 108 109 110
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 添加成员数据。 |

**返回值:**
L
linhaoran 已提交
111

Z
zengyawen 已提交
112 113 114
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功增加元素返回true,否则返回false。 |
L
linhaoran 已提交
115

Z
zengyawen 已提交
116 117
**示例:**

118
```ts
Z
zengyawen 已提交
119
let hashSet = new HashSet();
120
let result = hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
Z
zengyawen 已提交
121
```
L
linhaoran 已提交
122 123 124 125


### remove

Z
zengyawen 已提交
126
remove(value: T): boolean
L
linhaoran 已提交
127 128 129

删除指定的元素。

Z
zengyawen 已提交
130 131
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
132 133 134 135 136 137 138
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 指定删除的元素。 |

**返回值:**
L
linhaoran 已提交
139

Z
zengyawen 已提交
140 141 142
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功删除指定元素返回true,否则返回false。 |
L
linhaoran 已提交
143

Z
zengyawen 已提交
144 145
**示例:**

146
```ts
Z
zengyawen 已提交
147 148 149
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
150
let result = hashSet.remove("sdfs");
Z
zengyawen 已提交
151
```
L
linhaoran 已提交
152 153 154 155


### clear

Z
zengyawen 已提交
156
clear(): void
L
linhaoran 已提交
157 158 159

清除HashSet中的所有元素,并把length置为0。

Z
zengyawen 已提交
160 161
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
162 163
**示例:**

164
```ts
Z
zengyawen 已提交
165 166 167 168 169
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
hashSet.clear();
```
L
linhaoran 已提交
170 171 172 173


### values

Z
zengyawen 已提交
174
values(): IterableIterator<T>
L
linhaoran 已提交
175

W
wusongqing 已提交
176
返回包含此映射中包含的键值的新迭代器对象。
L
linhaoran 已提交
177

Z
zengyawen 已提交
178 179
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
180 181 182 183 184 185 186 187
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator<T> | 返回一个迭代器。 |

**示例:**

188
```ts
Z
zengyawen 已提交
189 190 191 192 193 194
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
let iter = hashSet.values();
let temp = iter.next().value;
while(temp != undefined) {
195
  console.log("value:" + temp);
Z
zengyawen 已提交
196 197 198
  temp = iter.next().value;
} 
```
L
linhaoran 已提交
199 200 201 202


### forEach

203
forEach(callbackfn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void
L
linhaoran 已提交
204 205 206

通过回调函数来遍历实例对象上的元素以及元素对应的下标。

Z
zengyawen 已提交
207 208
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
209
**参数:**
L
linhaoran 已提交
210

Z
zengyawen 已提交
211 212 213 214
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackfn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |
L
linhaoran 已提交
215

Z
zengyawen 已提交
216 217 218
callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
219
| value | T | 否 | 当前遍历到的元素键值对的值。 |
Z
zengyawen 已提交
220
| key | T | 否 | 当前遍历到的元素键值对的值(和value相同)。 |
221
| set | HashSet<T> | 否 | 当前调用forEach方法的实例对象。 |
L
linhaoran 已提交
222

Z
zengyawen 已提交
223 224
**示例:**

225
```ts
Z
zengyawen 已提交
226 227 228 229
let hashSet = new HashSet();
hashSet.add("sdfs");
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.forEach((value, key) => {
230
  console.log("value:" + value, key);
Z
zengyawen 已提交
231 232
});
```
L
linhaoran 已提交
233 234 235


### entries
Z
zengyawen 已提交
236
entries(): IterableIterator<[T, T]>
L
linhaoran 已提交
237

238
返回包含此映射中包含的键值对的新迭代器对象。
L
linhaoran 已提交
239

Z
zengyawen 已提交
240 241
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
242 243 244 245
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
246
| IterableIterator<[T, T]> | 返回一个迭代器。 |
Z
zengyawen 已提交
247 248 249

**示例:**

250
```ts
Z
zengyawen 已提交
251 252 253 254 255 256
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");
let iter = hashSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
257 258
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
259 260 261
  temp = iter.next().value;
}
```
L
linhaoran 已提交
262 263 264 265


### [Symbol.iterator]

Z
zengyawen 已提交
266
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
L
linhaoran 已提交
267

Z
zengyawen 已提交
268 269 270
返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。

**系统能力:** SystemCapability.Utils.Lang
L
linhaoran 已提交
271

Z
zengyawen 已提交
272 273 274 275 276 277 278 279
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;T&gt; | 返回一个迭代器 |

**示例:**

280
```ts
Z
zengyawen 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293
let hashSet = new HashSet();
hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashSet.add("sdfs");

// 使用方法一:
for (let item of hashSet) { 
  console.log("value: " + item);
}

// 使用方法二:
let iter = hashSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
294
  console.log("value: " + temp);
Z
zengyawen 已提交
295 296 297
  temp = iter.next().value;
}
```