1# @ohos.util.HashSet (非线性容器HashSet)
2
3HashSet基于[HashMap](js-apis-hashmap.md)实现。在HashSet中,只对value对象进行处理。
4
5HashSet和[TreeSet](js-apis-treeset.md)相比,HashSet中的数据无序存放,即存放元素的顺序和取出的顺序不一致,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不建议存放null值,可能会对排序结果产生影响。
6
7**推荐使用场景:** 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。
8
9文档中存在泛型的使用,涉及以下泛型标记符:<br>
10- T:Type,类
11
12> **说明:**
13>
14> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15
16
17## 导入模块
18
19```ts
20import { HashSet } from '@kit.ArkTS';
21```
22
23## HashSet
24
25### 属性
26
27**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
28
29**系统能力:** SystemCapability.Utils.Lang
30
31| 名称 | 类型 | 可读 | 可写 | 说明 |
32| -------- | -------- | -------- | -------- | -------- |
33| length | number | 是 | 否 | HashSet的元素个数。 |
34
35**示例:**
36
37```ts
38let hashSet: HashSet<number> = new HashSet();
39hashSet.add(1);
40hashSet.add(2);
41hashSet.add(3);
42hashSet.add(4);
43hashSet.add(5);
44let res = hashSet.length;
45```
46
47### constructor
48
49constructor()
50
51HashSet的构造函数。
52
53**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
54
55**系统能力:** SystemCapability.Utils.Lang
56
57**错误码:**
58
59以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
60
61| 错误码ID | 错误信息 |
62| -------- | -------- |
63| 10200012 | The HashSet's constructor cannot be directly invoked. |
64
65**示例:**
66
67```ts
68let hashSet: HashSet<number> = new HashSet();
69```
70
71
72### isEmpty
73
74isEmpty(): boolean
75
76判断该HashSet是否为空。
77
78**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
79
80**系统能力:** SystemCapability.Utils.Lang
81
82**返回值:**
83
84| 类型 | 说明 |
85| -------- | -------- |
86| boolean | 为空返回true,不为空返回false。 |
87
88**错误码:**
89
90以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
91
92| 错误码ID | 错误信息 |
93| -------- | -------- |
94| 10200011 | The isEmpty method cannot be bound. |
95
96**示例:**
97
98```ts
99const hashSet: HashSet<number> = new HashSet();
100let result = hashSet.isEmpty();
101```
102
103
104### has
105
106has(value: T): boolean
107
108判断此HashSet中是否含有该指定元素。
109
110**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
111
112**系统能力:** SystemCapability.Utils.Lang
113
114**参数:**
115
116| 参数名 | 类型 | 必填 | 说明 |
117| -------- | -------- | -------- | -------- |
118| value | T | 是 | 指定元素。 |
119
120**返回值:**
121
122| 类型 | 说明 |
123| -------- | -------- |
124| boolean | 包含指定元素返回true,否则返回false。 |
125
126**错误码:**
127
128以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
129
130| 错误码ID | 错误信息 |
131| -------- | -------- |
132| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
133| 10200011 | The has method cannot be bound. |
134
135**示例:**
136
137```ts
138let hashSet: HashSet<string> = new HashSet();
139hashSet.add("squirrel");
140let result = hashSet.has("squirrel");
141```
142
143
144### add
145
146add(value: T): boolean
147
148向HashSet中添加数据。
149
150**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
151
152**系统能力:** SystemCapability.Utils.Lang
153
154**参数:**
155
156| 参数名 | 类型 | 必填 | 说明 |
157| -------- | -------- | -------- | -------- |
158| value | T | 是 | 添加成员数据。 |
159
160**返回值:**
161
162| 类型 | 说明 |
163| -------- | -------- |
164| boolean | 成功增加元素返回true,否则返回false。 |
165
166**错误码:**
167
168以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
169
170| 错误码ID | 错误信息 |
171| -------- | -------- |
172| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
173| 10200011 | The add method cannot be bound. |
174
175**示例:**
176
177```ts
178let hashSet: HashSet<string> = new HashSet();
179let result = hashSet.add("squirrel");
180```
181
182
183### remove
184
185remove(value: T): boolean
186
187删除指定的元素。
188
189**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
190
191**系统能力:** SystemCapability.Utils.Lang
192
193**参数:**
194
195| 参数名 | 类型 | 必填 | 说明 |
196| -------- | -------- | -------- | -------- |
197| value | T | 是 | 指定删除的元素。 |
198
199**返回值:**
200
201| 类型 | 说明 |
202| -------- | -------- |
203| boolean | 成功删除指定元素返回true,否则返回false。 |
204
205**错误码:**
206
207以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
208
209| 错误码ID | 错误信息 |
210| -------- | -------- |
211| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
212| 10200011 | The remove method cannot be bound. |
213
214**示例:**
215
216```ts
217let hashSet: HashSet<string> = new HashSet();
218hashSet.add("squirrel");
219hashSet.add("sparrow");
220let result = hashSet.remove("sparrow");
221```
222
223
224### clear
225
226clear(): void
227
228清除HashSet中的所有元素,并把length置为0。
229
230**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
231
232**系统能力:** SystemCapability.Utils.Lang
233
234**错误码:**
235
236以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
237
238| 错误码ID | 错误信息 |
239| -------- | -------- |
240| 10200011 | The clear method cannot be bound. |
241
242**示例:**
243
244```ts
245let hashSet: HashSet<string> = new HashSet();
246hashSet.add("squirrel");
247hashSet.add("sparrow");
248hashSet.clear();
249```
250
251
252### values
253
254values(): IterableIterator&lt;T&gt;
255
256返回包含此映射中包含的键值的新迭代器对象。
257
258**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
259
260**系统能力:** SystemCapability.Utils.Lang
261
262**返回值:**
263
264| 类型 | 说明 |
265| -------- | -------- |
266| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
267
268**错误码:**
269
270以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
271
272| 错误码ID | 错误信息 |
273| -------- | -------- |
274| 10200011 | The values method cannot be bound. |
275
276**示例:**
277
278```ts
279let hashSet: HashSet<string> = new HashSet();
280hashSet.add("squirrel");
281hashSet.add("sparrow");
282let iter = hashSet.values();
283let temp = iter.next();
284while(!temp.done) {
285  console.log("value:" + temp.value);
286  temp = iter.next();
287}
288```
289
290
291### forEach
292
293forEach(callbackFn: (value?: T, key?: T, set?: HashSet&lt;T&gt;) => void, thisArg?: Object): void
294
295通过回调函数来遍历实例对象上的元素以及元素对应的下标。
296
297**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
298
299**系统能力:** SystemCapability.Utils.Lang
300
301**参数:**
302
303| 参数名 | 类型 | 必填 | 说明 |
304| -------- | -------- | -------- | -------- |
305| callbackFn | function | 是 | 回调函数。 |
306| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
307
308callbackfn的参数说明:
309| 参数名 | 类型 | 必填 | 说明 |
310| -------- | -------- | -------- | -------- |
311| value | T | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 |
312| key | T | 否 | 当前遍历到的元素键值对的键(和value相同),默认值为首个键值对的键。 |
313| set | HashSet&lt;T&gt; | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
314
315**错误码:**
316
317以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
318
319| 错误码ID | 错误信息 |
320| -------- | -------- |
321| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
322| 10200011 | The forEach method cannot be bound. |
323
324**示例:**
325
326```ts
327let hashSet: HashSet<string> = new HashSet();
328hashSet.add("sparrow");
329hashSet.add("squirrel");
330hashSet.forEach((value?: string, key?: string): void => {
331  console.log("value:" + value, "key:" + key);
332});
333```
334```ts
335// 不建议在forEach中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
336let hashSet : HashSet<string> = new HashSet();
337for(let i = 0;i < 10; i++) {
338  hashSet.add("sparrow" + i);
339}
340for(let i = 0;i < 10; i++) {
341  hashSet.remove("sparrow" + i);
342}
343```
344
345### entries
346entries(): IterableIterator<[T, T]>
347
348返回包含此映射中包含的键值对的新迭代器对象。
349
350**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
351
352**系统能力:** SystemCapability.Utils.Lang
353
354**返回值:**
355
356| 类型 | 说明 |
357| -------- | -------- |
358| IterableIterator<[T, T]> | 返回一个迭代器。 |
359
360**错误码:**
361
362以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
363
364| 错误码ID | 错误信息 |
365| -------- | -------- |
366| 10200011 | The entries method cannot be bound. |
367
368**示例:**
369
370```ts
371let hashSet: HashSet<string> = new HashSet();
372hashSet.add("squirrel");
373hashSet.add("sparrow");
374let iter = hashSet.entries();
375let temp: IteratorResult<[string, string]> = iter.next();
376while(!temp.done) {
377  console.log("key:" + temp.value[0]);
378  console.log("value:" + temp.value[1]);
379  temp = iter.next();
380}
381```
382```ts
383// 不建议在entries中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
384let hashSet : HashSet<string> = new HashSet();
385for(let i = 0;i < 10; i++) {
386  hashSet.add("sparrow" + i);
387}
388for(let i = 0;i < 10; i++) {
389  hashSet.remove("sparrow" + i);
390}
391```
392
393### [Symbol.iterator]
394
395[Symbol.iterator]\(): IterableIterator&lt;T&gt;
396
397返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
398
399**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
400
401**系统能力:** SystemCapability.Utils.Lang
402
403**返回值:**
404
405| 类型 | 说明 |
406| -------- | -------- |
407| IterableIterator&lt;T&gt; | 返回一个迭代器 |
408
409**错误码:**
410
411以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
412
413| 错误码ID | 错误信息 |
414| -------- | -------- |
415| 10200011 | The Symbol.iterator method cannot be bound. |
416
417**示例:**
418
419```ts
420let hashSet: HashSet<string> = new HashSet();
421hashSet.add("squirrel");
422hashSet.add("sparrow");
423
424// 使用方法一:
425let val: Array<string> = Array.from(hashSet.values())
426for (let item of val) {
427  console.log("value: " + item);
428}
429
430// 使用方法二:
431let iter = hashSet[Symbol.iterator]();
432let temp: IteratorResult<string> = iter.next();
433while(!temp.done) {
434  console.log("value: " + temp.value);
435  temp = iter.next();
436}
437```
438```ts
439// 不建议在Symbol.iterator中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
440let hashSet : HashSet<string> = new HashSet();
441for(let i = 0;i < 10;i++) {
442  hashSet.add("sparrow" + i);
443}
444for(let i = 0;i < 10;i++) {
445  hashSet.remove("sparrow" + i);
446}
447```