• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.HashSet (Nonlinear Container HashSet)
2
3**HashSet** is implemented based on [HashMap](js-apis-hashmap.md). In **HashSet**, only the **value** object is processed.
4
5Unlike [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 allows only unique elements. However, null values are allowed in **HashSet**, but not allowed in **TreeSet**.
6
7**Recommended use case**: Use **HashSet** when you need a set that has only unique elements or need to deduplicate a set.
8
9This topic uses the following to identify the use of generics:
10- T: Type
11
12> **NOTE**
13>
14> 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.
15
16
17## Modules to Import
18
19```ts
20import HashSet from '@ohos.util.HashSet';
21```
22
23## HashSet
24
25### Attributes
26
27**System capability**: SystemCapability.Utils.Lang
28
29| Name| Type| Readable| Writable| Description|
30| -------- | -------- | -------- | -------- | -------- |
31| length | number | Yes| No| Number of elements in a hash set (called container later).|
32
33**Example**
34
35```ts
36let hashSet = new HashSet();
37hashSet.add(1);
38hashSet.add(2);
39hashSet.add(3);
40hashSet.add(4);
41hashSet.add(5);
42let res = hashSet.length;
43```
44
45### constructor
46
47constructor()
48
49A constructor used to create a **HashSet** instance.
50
51**System capability**: SystemCapability.Utils.Lang
52
53**Error codes**
54
55For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
56
57| ID| Error Message|
58| -------- | -------- |
59| 10200012 | The HashSet's constructor cannot be directly invoked. |
60
61**Example**
62
63```ts
64let hashSet = new HashSet();
65```
66
67
68### isEmpty
69
70isEmpty(): boolean
71
72Checks whether this container is empty (contains no element).
73
74**System capability**: SystemCapability.Utils.Lang
75
76**Return value**
77
78| Type| Description|
79| -------- | -------- |
80| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
81
82**Error codes**
83
84For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
85
86| ID| Error Message|
87| -------- | -------- |
88| 10200011 | The isEmpty method cannot be bound. |
89
90**Example**
91
92```ts
93const hashSet = new HashSet();
94let result = hashSet.isEmpty();
95```
96
97
98### has
99
100has(value: T): boolean
101
102Checks whether this container contains the specified element.
103
104**System capability**: SystemCapability.Utils.Lang
105
106**Parameters**
107
108| Name| Type| Mandatory| Description|
109| -------- | -------- | -------- | -------- |
110| value | T | Yes| Target element.|
111
112**Return value**
113
114| Type| Description|
115| -------- | -------- |
116| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
117
118**Error codes**
119
120For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
121
122| ID| Error Message|
123| -------- | -------- |
124| 10200011 | The has method cannot be bound. |
125
126**Example**
127
128```ts
129let hashSet = new HashSet();
130hashSet.add("squirrel");
131let result = hashSet.has("squirrel");
132```
133
134
135### add
136
137add(value: T): boolean
138
139Adds an element to this container.
140
141**System capability**: SystemCapability.Utils.Lang
142
143**Parameters**
144
145| Name| Type| Mandatory| Description|
146| -------- | -------- | -------- | -------- |
147| value | T | Yes| Target element.|
148
149**Return value**
150
151| Type| Description|
152| -------- | -------- |
153| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
154
155**Error codes**
156
157For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
158
159| ID| Error Message|
160| -------- | -------- |
161| 10200011 | The add method cannot be bound. |
162
163**Example**
164
165```ts
166let hashSet = new HashSet();
167let result = hashSet.add("squirrel");
168```
169
170
171### remove
172
173remove(value: T): boolean
174
175Removes an element from this container.
176
177**System capability**: SystemCapability.Utils.Lang
178
179**Parameters**
180
181| Name| Type| Mandatory| Description|
182| -------- | -------- | -------- | -------- |
183| value | T | Yes| Target element.|
184
185**Return value**
186
187| Type| Description|
188| -------- | -------- |
189| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
190
191**Error codes**
192
193For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
194
195| ID| Error Message|
196| -------- | -------- |
197| 10200011 | The remove method cannot be bound. |
198
199**Example**
200
201```ts
202let hashSet = new HashSet();
203hashSet.add("squirrel");
204hashSet.add("sparrow");
205let result = hashSet.remove("sparrow");
206```
207
208
209### clear
210
211clear(): void
212
213Clears this container and sets its length to **0**.
214
215**System capability**: SystemCapability.Utils.Lang
216
217**Error codes**
218
219For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
220
221| ID| Error Message|
222| -------- | -------- |
223| 10200011 | The clear method cannot be bound. |
224
225**Example**
226
227```ts
228let hashSet = new HashSet();
229hashSet.add("squirrel");
230hashSet.add("sparrow");
231hashSet.clear();
232```
233
234
235### values
236
237values(): IterableIterator<T>
238
239Obtains an iterator that contains all the values in this container.
240
241**System capability**: SystemCapability.Utils.Lang
242
243**Return value**
244
245| Type| Description|
246| -------- | -------- |
247| IterableIterator<T> | Iterator obtained.|
248
249**Error codes**
250
251For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
252
253| ID| Error Message|
254| -------- | -------- |
255| 10200011 | The values method cannot be bound. |
256
257**Example**
258
259```ts
260let hashSet = new HashSet();
261hashSet.add("squirrel");
262hashSet.add("sparrow");
263let iter = hashSet.values();
264let temp = iter.next().value;
265while(temp != undefined) {
266  console.log("value:" + temp);
267  temp = iter.next().value;
268}
269```
270
271
272### forEach
273
274forEach(callbackFn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void
275
276Uses a callback to traverse the elements in this container and obtain their position indexes.
277
278**System capability**: SystemCapability.Utils.Lang
279
280**Parameters**
281
282| Name| Type| Mandatory| Description|
283| -------- | -------- | -------- | -------- |
284| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
285| thisArg | Object | No| Value to use when the callback is invoked.|
286
287callbackfn
288| Name| Type| Mandatory| Description|
289| -------- | -------- | -------- | -------- |
290| value | T | No| Value of the element that is currently traversed.|
291| key | T | No| Key of the element that is currently traversed (same as **value**).|
292| set | HashSet<T> | No| Instance that invokes the **forEach** API.|
293
294**Error codes**
295
296For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
297
298| ID| Error Message|
299| -------- | -------- |
300| 10200011 | The forEach method cannot be bound. |
301
302**Example**
303
304```ts
305let hashSet = new HashSet();
306hashSet.add("sparrow");
307hashSet.add("squirrel");
308hashSet.forEach((value, key) => {
309    console.log("value:" + value, "key:" + key);
310});
311```
312
313
314### entries
315entries(): IterableIterator<[T, T]>
316
317Obtains an iterator that contains all the elements in this container.
318
319**System capability**: SystemCapability.Utils.Lang
320
321**Return value**
322
323| Type| Description|
324| -------- | -------- |
325| IterableIterator<[T, T]> | Iterator obtained.|
326
327**Error codes**
328
329For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
330
331| ID| Error Message|
332| -------- | -------- |
333| 10200011 | The entries method cannot be bound. |
334
335**Example**
336
337```ts
338let hashSet = new HashSet();
339hashSet.add("squirrel");
340hashSet.add("sparrow");
341let iter = hashSet.entries();
342let temp = iter.next().value;
343while(temp != undefined) {
344  console.log("key:" + temp[0]);
345  console.log("value:" + temp[1]);
346  temp = iter.next().value;
347}
348```
349
350
351### [Symbol.iterator]
352
353[Symbol.iterator]\(): IterableIterator&lt;T&gt;
354
355Obtains an iterator, each item of which is a JavaScript object.
356
357**System capability**: SystemCapability.Utils.Lang
358
359**Return value**
360
361| Type| Description|
362| -------- | -------- |
363| IterableIterator&lt;T&gt; | Iterator obtained.|
364
365**Error codes**
366
367For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
368
369| ID| Error Message|
370| -------- | -------- |
371| 10200011 | The Symbol.iterator method cannot be bound. |
372
373**Example**
374
375```ts
376let hashSet = new HashSet();
377hashSet.add("squirrel");
378hashSet.add("sparrow");
379
380// Method 1:
381for (let item of hashSet) {
382  console.log("value: " + item);
383}
384
385// Method 2:
386let iter = hashSet[Symbol.iterator]();
387let temp = iter.next().value;
388while(temp != undefined) {
389  console.log("value: " + temp);
390  temp = iter.next().value;
391}
392```
393