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