1e41f4b71Sopenharmony_ci# @ohos.util.HashSet (Nonlinear Container HashSet)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci**HashSet** is implemented based on [HashMap](js-apis-hashmap.md). In **HashSet**, only the **value** object is processed.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciUnlike [TreeSet](js-apis-treeset.md), which stores and accesses data in sorted order, **HashSet** stores data in a random order. This means that **HashSet** may use a different order when storing and accessing elements. Both of them allow only unique elements. However, null values are allowed in **HashSet**, but not in **TreeSet**, because null values may affect the order of elements in the container.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Recommended use case**: Use **HashSet** when you need a set that has only unique elements or need to deduplicate a set.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciThis topic uses the following to identify the use of generics:
10e41f4b71Sopenharmony_ci- T: Type
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci> **NOTE**
13e41f4b71Sopenharmony_ci>
14e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## Modules to Import
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci```ts
20e41f4b71Sopenharmony_ciimport { HashSet } from '@kit.ArkTS';
21e41f4b71Sopenharmony_ci```
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci## HashSet
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci### Attributes
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description |
32e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
33e41f4b71Sopenharmony_ci| length | number | Yes | No | Number of elements in a hash set (called container later). |
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Example**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci```ts
38e41f4b71Sopenharmony_cilet hashSet: HashSet<number> = new HashSet();
39e41f4b71Sopenharmony_cihashSet.add(1);
40e41f4b71Sopenharmony_cihashSet.add(2);
41e41f4b71Sopenharmony_cihashSet.add(3);
42e41f4b71Sopenharmony_cihashSet.add(4);
43e41f4b71Sopenharmony_cihashSet.add(5);
44e41f4b71Sopenharmony_cilet res = hashSet.length;
45e41f4b71Sopenharmony_ci```
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci### constructor
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciconstructor()
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciA constructor used to create a **HashSet** instance.
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci**Error codes**
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci| ID | Error Message |
62e41f4b71Sopenharmony_ci| -------- | -------- |
63e41f4b71Sopenharmony_ci| 10200012 | The HashSet's constructor cannot be directly invoked. |
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci**Example**
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci```ts
68e41f4b71Sopenharmony_cilet hashSet: HashSet<number> = new HashSet();
69e41f4b71Sopenharmony_ci```
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci### isEmpty
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ciisEmpty(): boolean
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ciChecks whether this container is empty (contains no element).
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**Return value**
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci| Type | Description |
85e41f4b71Sopenharmony_ci| -------- | -------- |
86e41f4b71Sopenharmony_ci| boolean | Returns **true** if the container is empty; returns **false** otherwise. |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Error codes**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| ID | Error Message |
93e41f4b71Sopenharmony_ci| -------- | -------- |
94e41f4b71Sopenharmony_ci| 10200011 | The isEmpty method cannot be bound. |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**Example**
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci```ts
99e41f4b71Sopenharmony_ciconst hashSet: HashSet<number> = new HashSet();
100e41f4b71Sopenharmony_cilet result = hashSet.isEmpty();
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci### has
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_cihas(value: T): boolean
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ciChecks whether this container contains the specified element.
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ci**Parameters**
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
117e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
118e41f4b71Sopenharmony_ci| value | T | Yes | Target element. |
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci**Return value**
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci| Type | Description |
123e41f4b71Sopenharmony_ci| -------- | -------- |
124e41f4b71Sopenharmony_ci| boolean | Returns **true** if the specified element is contained; returns **false** otherwise. |
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**Error codes**
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci| ID | Error Message |
131e41f4b71Sopenharmony_ci| -------- | -------- |
132e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
133e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound. |
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci**Example**
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci```ts
138e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
139e41f4b71Sopenharmony_cihashSet.add("squirrel");
140e41f4b71Sopenharmony_cilet result = hashSet.has("squirrel");
141e41f4b71Sopenharmony_ci```
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci### add
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ciadd(value: T): boolean
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ciAdds an element to this container.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**Parameters**
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
157e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
158e41f4b71Sopenharmony_ci| value | T | Yes | Target element. |
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci**Return value**
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci| Type | Description |
163e41f4b71Sopenharmony_ci| -------- | -------- |
164e41f4b71Sopenharmony_ci| boolean | Returns **true** if the element is added successfully; returns **false** otherwise. |
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci**Error codes**
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci| ID | Error Message |
171e41f4b71Sopenharmony_ci| -------- | -------- |
172e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
173e41f4b71Sopenharmony_ci| 10200011 | The add method cannot be bound. |
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**Example**
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci```ts
178e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
179e41f4b71Sopenharmony_cilet result = hashSet.add("squirrel");
180e41f4b71Sopenharmony_ci```
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci### remove
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ciremove(value: T): boolean
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ciRemoves an element from this container.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**Parameters**
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
196e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
197e41f4b71Sopenharmony_ci| value | T | Yes | Target element. |
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci**Return value**
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci| Type | Description |
202e41f4b71Sopenharmony_ci| -------- | -------- |
203e41f4b71Sopenharmony_ci| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise. |
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci**Error codes**
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci| ID | Error Message |
210e41f4b71Sopenharmony_ci| -------- | -------- |
211e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
212e41f4b71Sopenharmony_ci| 10200011 | The remove method cannot be bound. |
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci**Example**
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci```ts
217e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
218e41f4b71Sopenharmony_cihashSet.add("squirrel");
219e41f4b71Sopenharmony_cihashSet.add("sparrow");
220e41f4b71Sopenharmony_cilet result = hashSet.remove("sparrow");
221e41f4b71Sopenharmony_ci```
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci### clear
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ciclear(): void
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ciClears this container and sets its length to **0**.
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci**Error codes**
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci| ID | Error Message |
239e41f4b71Sopenharmony_ci| -------- | -------- |
240e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound. |
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**Example**
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci```ts
245e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
246e41f4b71Sopenharmony_cihashSet.add("squirrel");
247e41f4b71Sopenharmony_cihashSet.add("sparrow");
248e41f4b71Sopenharmony_cihashSet.clear();
249e41f4b71Sopenharmony_ci```
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ci
252e41f4b71Sopenharmony_ci### values
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_civalues(): IterableIterator&lt;T&gt;
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ciObtains an iterator that contains all the values in this container.
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci**Return value**
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci| Type | Description |
265e41f4b71Sopenharmony_ci| -------- | -------- |
266e41f4b71Sopenharmony_ci| IterableIterator&lt;T&gt; | Iterator obtained. |
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci**Error codes**
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci| ID | Error Message |
273e41f4b71Sopenharmony_ci| -------- | -------- |
274e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. |
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci**Example**
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci```ts
279e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
280e41f4b71Sopenharmony_cihashSet.add("squirrel");
281e41f4b71Sopenharmony_cihashSet.add("sparrow");
282e41f4b71Sopenharmony_cilet iter = hashSet.values();
283e41f4b71Sopenharmony_cilet temp = iter.next();
284e41f4b71Sopenharmony_ciwhile(!temp.done) {
285e41f4b71Sopenharmony_ci  console.log("value:" + temp.value);
286e41f4b71Sopenharmony_ci  temp = iter.next();
287e41f4b71Sopenharmony_ci}
288e41f4b71Sopenharmony_ci```
289e41f4b71Sopenharmony_ci
290e41f4b71Sopenharmony_ci
291e41f4b71Sopenharmony_ci### forEach
292e41f4b71Sopenharmony_ci
293e41f4b71Sopenharmony_ciforEach(callbackFn: (value?: T, key?: T, set?: HashSet&lt;T&gt;) => void, thisArg?: Object): void
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ciUses a callback to traverse the elements in this container and obtain their position indexes.
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
300e41f4b71Sopenharmony_ci
301e41f4b71Sopenharmony_ci**Parameters**
302e41f4b71Sopenharmony_ci
303e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
304e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
305e41f4b71Sopenharmony_ci| callbackFn | function | Yes | Callback invoked to traverse the elements in the container. |
306e41f4b71Sopenharmony_ci| thisArg | Object | No | Value of **this** to use when **callbackFn** is invoked. The default value is this instance. |
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_cicallbackFn
309e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
310e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
311e41f4b71Sopenharmony_ci| value | T | No | Value of the element that is currently traversed. The default value is the value of the first key-value pair. |
312e41f4b71Sopenharmony_ci| key | T | No | Key of the element that is currently traversed (same as **value**). The default value is the key of the first key-value pair. |
313e41f4b71Sopenharmony_ci| set | HashSet&lt;T&gt; | No | Instance that calls the **forEach** API. The default value is this instance. |
314e41f4b71Sopenharmony_ci
315e41f4b71Sopenharmony_ci**Error codes**
316e41f4b71Sopenharmony_ci
317e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
318e41f4b71Sopenharmony_ci
319e41f4b71Sopenharmony_ci| ID | Error Message |
320e41f4b71Sopenharmony_ci| -------- | -------- |
321e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
322e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. |
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci**Example**
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci```ts
327e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
328e41f4b71Sopenharmony_cihashSet.add("sparrow");
329e41f4b71Sopenharmony_cihashSet.add("squirrel");
330e41f4b71Sopenharmony_cihashSet.forEach((value?: string, key?: string): void => {
331e41f4b71Sopenharmony_ci  console.log("value:" + value, "key:" + key);
332e41f4b71Sopenharmony_ci});
333e41f4b71Sopenharmony_ci```
334e41f4b71Sopenharmony_ci```ts
335e41f4b71Sopenharmony_ci// You are not advised to use the set or remove APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
336e41f4b71Sopenharmony_cilet hashSet : HashSet<string> = new HashSet();
337e41f4b71Sopenharmony_cifor(let i = 0;i < 10; i++) {
338e41f4b71Sopenharmony_ci  hashSet.add("sparrow" + i);
339e41f4b71Sopenharmony_ci}
340e41f4b71Sopenharmony_cifor(let i = 0;i < 10; i++) {
341e41f4b71Sopenharmony_ci  hashSet.remove("sparrow" + i);
342e41f4b71Sopenharmony_ci}
343e41f4b71Sopenharmony_ci```
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci### entries
346e41f4b71Sopenharmony_cientries(): IterableIterator<[T, T]>
347e41f4b71Sopenharmony_ci
348e41f4b71Sopenharmony_ciObtains an iterator that contains all the elements in this container.
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
353e41f4b71Sopenharmony_ci
354e41f4b71Sopenharmony_ci**Return value**
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci| Type | Description |
357e41f4b71Sopenharmony_ci| -------- | -------- |
358e41f4b71Sopenharmony_ci| IterableIterator<[T, T]> | Iterator obtained. |
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci**Error codes**
361e41f4b71Sopenharmony_ci
362e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci| ID | Error Message |
365e41f4b71Sopenharmony_ci| -------- | -------- |
366e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound. |
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci**Example**
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci```ts
371e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
372e41f4b71Sopenharmony_cihashSet.add("squirrel");
373e41f4b71Sopenharmony_cihashSet.add("sparrow");
374e41f4b71Sopenharmony_cilet iter = hashSet.entries();
375e41f4b71Sopenharmony_cilet temp: IteratorResult<[string, string]> = iter.next();
376e41f4b71Sopenharmony_ciwhile(!temp.done) {
377e41f4b71Sopenharmony_ci  console.log("key:" + temp.value[0]);
378e41f4b71Sopenharmony_ci  console.log("value:" + temp.value[1]);
379e41f4b71Sopenharmony_ci  temp = iter.next();
380e41f4b71Sopenharmony_ci}
381e41f4b71Sopenharmony_ci```
382e41f4b71Sopenharmony_ci```ts
383e41f4b71Sopenharmony_ci// You are not advised to use the set or remove APIs in entries because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
384e41f4b71Sopenharmony_cilet hashSet : HashSet<string> = new HashSet();
385e41f4b71Sopenharmony_cifor(let i = 0;i < 10; i++) {
386e41f4b71Sopenharmony_ci  hashSet.add("sparrow" + i);
387e41f4b71Sopenharmony_ci}
388e41f4b71Sopenharmony_cifor(let i = 0;i < 10; i++) {
389e41f4b71Sopenharmony_ci  hashSet.remove("sparrow" + i);
390e41f4b71Sopenharmony_ci}
391e41f4b71Sopenharmony_ci```
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci### [Symbol.iterator]
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator&lt;T&gt;
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object.
398e41f4b71Sopenharmony_ci
399e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci> **NOTE**
404e41f4b71Sopenharmony_ci>
405e41f4b71Sopenharmony_ci> This API cannot be used in .ets files.
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci**Return value**
408e41f4b71Sopenharmony_ci
409e41f4b71Sopenharmony_ci| Type | Description |
410e41f4b71Sopenharmony_ci| -------- | -------- |
411e41f4b71Sopenharmony_ci| IterableIterator&lt;T&gt; | Iterator obtained. |
412e41f4b71Sopenharmony_ci
413e41f4b71Sopenharmony_ci**Error codes**
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
416e41f4b71Sopenharmony_ci
417e41f4b71Sopenharmony_ci| ID | Error Message |
418e41f4b71Sopenharmony_ci| -------- | -------- |
419e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. |
420e41f4b71Sopenharmony_ci
421e41f4b71Sopenharmony_ci**Example**
422e41f4b71Sopenharmony_ci
423e41f4b71Sopenharmony_ci```ts
424e41f4b71Sopenharmony_cilet hashSet: HashSet<string> = new HashSet();
425e41f4b71Sopenharmony_cihashSet.add("squirrel");
426e41f4b71Sopenharmony_cihashSet.add("sparrow");
427e41f4b71Sopenharmony_ci
428e41f4b71Sopenharmony_ci// Method 1:
429e41f4b71Sopenharmony_cilet val: Array<string> = Array.from(hashSet.values())
430e41f4b71Sopenharmony_cifor (let item of val) {
431e41f4b71Sopenharmony_ci  console.log("value: " + item);
432e41f4b71Sopenharmony_ci}
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci// Method 2:
435e41f4b71Sopenharmony_cilet iter = hashSet[Symbol.iterator]();
436e41f4b71Sopenharmony_cilet temp: IteratorResult<string> = iter.next();
437e41f4b71Sopenharmony_ciwhile(!temp.done) {
438e41f4b71Sopenharmony_ci  console.log("value: " + temp.value);
439e41f4b71Sopenharmony_ci  temp = iter.next();
440e41f4b71Sopenharmony_ci}
441e41f4b71Sopenharmony_ci```
442e41f4b71Sopenharmony_ci```ts
443e41f4b71Sopenharmony_ci// You are not advised to use the set or remove APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
444e41f4b71Sopenharmony_cilet hashSet : HashSet<string> = new HashSet();
445e41f4b71Sopenharmony_cifor(let i = 0;i < 10;i++) {
446e41f4b71Sopenharmony_ci  hashSet.add("sparrow" + i);
447e41f4b71Sopenharmony_ci}
448e41f4b71Sopenharmony_cifor(let i = 0;i < 10;i++) {
449e41f4b71Sopenharmony_ci  hashSet.remove("sparrow" + i);
450e41f4b71Sopenharmony_ci}
451e41f4b71Sopenharmony_ci```
452