js-apis-hashset.md 8.6 KB
Newer Older
1
# @ohos.util.HashSet (Nonlinear Container HashSet)
W
wusongqing 已提交
2

W
wusongqing 已提交
3
> **NOTE**
W
wusongqing 已提交
4 5
> 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.

W
wusongqing 已提交
6 7 8 9 10
**HashSet** is implemented based on [HashMap](js-apis-hashmap.md). In **HashSet**, only the **value** object is processed.

Unlike [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**.

**Recommended use case**: Use **HashSet** when you need a set that has only unique elements or need to deduplicate a set.
W
wusongqing 已提交
11

G
Gloria 已提交
12 13 14
This topic uses the following to identify the use of generics:
- T: Type

W
wusongqing 已提交
15 16
## Modules to Import

W
wusongqing 已提交
17
```ts
W
wusongqing 已提交
18 19 20 21 22 23 24
import HashSet from '@ohos.util.HashSet';
```

## HashSet

### Attributes

W
wusongqing 已提交
25 26
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
27 28
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
29
| length | number | Yes| No| Number of elements in a hash set (called container later).|
W
wusongqing 已提交
30

G
Gloria 已提交
31 32 33 34 35 36 37 38 39 40 41
**Example**

```ts
let hashSet = new HashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
let res = hashSet.length;
```
W
wusongqing 已提交
42 43 44 45 46 47 48

### constructor

constructor()

A constructor used to create a **HashSet** instance.

W
wusongqing 已提交
49 50
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
51 52
**Error codes**

53
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
54 55 56 57 58

| ID| Error Message|
| -------- | -------- |
| 10200012 | The HashSet's constructor cannot be directly invoked. |

W
wusongqing 已提交
59 60
**Example**

W
wusongqing 已提交
61
```ts
W
wusongqing 已提交
62 63 64 65 66 67 68 69
let hashSet = new HashSet();
```


### isEmpty

isEmpty(): boolean

W
wusongqing 已提交
70 71 72
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
73 74 75 76 77 78 79

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

G
Gloria 已提交
80 81
**Error codes**

82
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
83 84 85 86 87

| ID| Error Message|
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

W
wusongqing 已提交
88 89
**Example**

W
wusongqing 已提交
90
```ts
W
wusongqing 已提交
91
const hashSet = new HashSet();
W
wusongqing 已提交
92
let result = hashSet.isEmpty();
W
wusongqing 已提交
93 94 95 96 97 98 99
```


### has

has(value: T): boolean

W
wusongqing 已提交
100 101 102
Checks whether this container contains the specified element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
103 104 105 106 107

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
108
| value | T | Yes| Target element.|
W
wusongqing 已提交
109 110 111 112 113

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
114
| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
W
wusongqing 已提交
115

G
Gloria 已提交
116 117
**Error codes**

118
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
119 120 121 122 123

| ID| Error Message|
| -------- | -------- |
| 10200011 | The has method cannot be bound. |

W
wusongqing 已提交
124 125
**Example**

W
wusongqing 已提交
126
```ts
W
wusongqing 已提交
127
let hashSet = new HashSet();
G
Gloria 已提交
128 129 130
let result = hashSet.has("squirrel");
hashSet.add("squirrel");
let result1 = hashSet.has("squirrel");
W
wusongqing 已提交
131 132 133 134 135 136 137
```


### add

add(value: T): boolean

W
wusongqing 已提交
138 139 140
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
141 142 143 144 145

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
146
| value | T | Yes| Target element.|
W
wusongqing 已提交
147 148 149 150 151

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
152
| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
W
wusongqing 已提交
153

G
Gloria 已提交
154 155
**Error codes**

156
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
157 158 159 160 161

| ID| Error Message|
| -------- | -------- |
| 10200011 | The add method cannot be bound. |

W
wusongqing 已提交
162 163
**Example**

W
wusongqing 已提交
164
```ts
W
wusongqing 已提交
165
let hashSet = new HashSet();
G
Gloria 已提交
166
let result = hashSet.add("squirrel");
W
wusongqing 已提交
167 168 169 170 171 172 173
```


### remove

remove(value: T): boolean

W
wusongqing 已提交
174 175 176
Removes an element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
177 178 179 180 181

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
182
| value | T | Yes| Target element.|
W
wusongqing 已提交
183 184 185 186 187

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
188
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
189

G
Gloria 已提交
190 191
**Error codes**

192
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
193 194 195 196 197

| ID| Error Message|
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

W
wusongqing 已提交
198 199
**Example**

W
wusongqing 已提交
200
```ts
W
wusongqing 已提交
201
let hashSet = new HashSet();
G
Gloria 已提交
202 203 204
hashSet.add("squirrel");
hashSet.add("sparrow");
let result = hashSet.remove("sparrow");
W
wusongqing 已提交
205 206 207 208 209 210 211 212 213
```


### clear

clear(): void

Clears this container and sets its length to **0**.

W
wusongqing 已提交
214 215
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
216 217
**Error codes**

218
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
219 220 221 222 223

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

W
wusongqing 已提交
224 225
**Example**

W
wusongqing 已提交
226
```ts
W
wusongqing 已提交
227
let hashSet = new HashSet();
G
Gloria 已提交
228 229
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
230 231 232 233 234 235 236 237 238 239
hashSet.clear();
```


### values

values(): IterableIterator<T>

Obtains an iterator that contains all the values in this container.

W
wusongqing 已提交
240 241
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
242 243 244 245 246 247
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|

G
Gloria 已提交
248 249
**Error codes**

250
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
251 252 253 254 255

| ID| Error Message|
| -------- | -------- |
| 10200011 | The values method cannot be bound. |

W
wusongqing 已提交
256 257
**Example**

W
wusongqing 已提交
258
```ts
W
wusongqing 已提交
259
let hashSet = new HashSet();
G
Gloria 已提交
260 261
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
262 263 264
let iter = hashSet.values();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
265
  console.log("value:" + temp);
W
wusongqing 已提交
266
  temp = iter.next().value;
G
Gloria 已提交
267
}
W
wusongqing 已提交
268 269 270 271 272
```


### forEach

273
forEach(callbackFn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
274

W
wusongqing 已提交
275 276 277
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
278 279 280 281 282

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
283
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
284 285 286 287 288
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
289 290
| value | T | No| Value of the element that is currently traversed.|
| key | T | No| Key of the element that is currently traversed (same as **value**).|
291
| set | HashSet<T> | No| Instance that invokes the **forEach** API.|
W
wusongqing 已提交
292

G
Gloria 已提交
293 294
**Error codes**

295
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
296 297 298 299 300

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
301 302
**Example**

W
wusongqing 已提交
303
```ts
W
wusongqing 已提交
304
let hashSet = new HashSet();
G
Gloria 已提交
305 306
hashSet.add("sparrow");
hashSet.add("squirrel");
W
wusongqing 已提交
307
hashSet.forEach((value, key) => {
W
wusongqing 已提交
308
  console.log("value:" + value, key);
W
wusongqing 已提交
309 310 311 312 313 314 315
});
```


### entries
entries(): IterableIterator<[T, T]>

W
wusongqing 已提交
316 317 318
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
319 320 321 322 323 324 325

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<[T, T]> | Iterator obtained.|

G
Gloria 已提交
326 327
**Error codes**

328
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
329 330 331 332 333

| ID| Error Message|
| -------- | -------- |
| 10200011 | The entries method cannot be bound. |

W
wusongqing 已提交
334 335
**Example**

W
wusongqing 已提交
336
```ts
W
wusongqing 已提交
337
let hashSet = new HashSet();
G
Gloria 已提交
338 339
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
340 341 342
let iter = hashSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
343 344
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
345 346 347 348 349 350 351 352 353 354 355
  temp = iter.next().value;
}
```


### [Symbol.iterator]

[Symbol.iterator]\(): IterableIterator&lt;T&gt;

Obtains an iterator, each item of which is a JavaScript object.

W
wusongqing 已提交
356 357
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
358 359 360 361 362 363
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|

G
Gloria 已提交
364 365
**Error codes**

366
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
367 368 369 370 371

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
372 373
**Example**

W
wusongqing 已提交
374
```ts
W
wusongqing 已提交
375
let hashSet = new HashSet();
G
Gloria 已提交
376 377
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
378 379 380 381 382 383 384 385 386 387

// Method 1:
for (let item of hashSet) { 
  console.log("value: " + item);
}

// Method 2:
let iter = hashSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
388
  console.log("value: " + temp);
W
wusongqing 已提交
389 390 391
  temp = iter.next().value;
}
```