1# @ohos.util.HashSet (Nonlinear Container HashSet) 2 3HashSet 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 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. 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 '@kit.ArkTS'; 21``` 22 23## HashSet 24 25### Properties 26 27**Atomic service API**: This API can be used in atomic services since API version 12. 28 29**System capability**: SystemCapability.Utils.Lang 30 31| Name| Type| Readable| Writable| Description| 32| -------- | -------- | -------- | -------- | -------- | 33| length | number | Yes| No| Number of elements in a HashSet.| 34 35**Example** 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 51A constructor used to create a **HashSet** instance. 52 53**Atomic service API**: This API can be used in atomic services since API version 12. 54 55**System capability**: SystemCapability.Utils.Lang 56 57**Error codes** 58 59For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 60 61| ID| Error Message| 62| -------- | -------- | 63| 10200012 | The HashSet's constructor cannot be directly invoked. | 64 65**Example** 66 67```ts 68let hashSet: HashSet<number> = new HashSet(); 69``` 70 71 72### isEmpty 73 74isEmpty(): boolean 75 76Checks whether this HashSet is empty (contains no element). 77 78**Atomic service API**: This API can be used in atomic services since API version 12. 79 80**System capability**: SystemCapability.Utils.Lang 81 82**Return value** 83 84| Type| Description| 85| -------- | -------- | 86| boolean | Check result. The value **true** is returned if the HashSet is empty; otherwise, **false** is returned.| 87 88**Error codes** 89 90For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 91 92| ID| Error Message| 93| -------- | -------- | 94| 10200011 | The isEmpty method cannot be bound. | 95 96**Example** 97 98```ts 99const hashSet: HashSet<number> = new HashSet(); 100let result = hashSet.isEmpty(); 101``` 102 103 104### has 105 106has(value: T): boolean 107 108Checks whether this HashSet has the specified element. 109 110**Atomic service API**: This API can be used in atomic services since API version 12. 111 112**System capability**: SystemCapability.Utils.Lang 113 114**Parameters** 115 116| Name| Type| Mandatory| Description| 117| -------- | -------- | -------- | -------- | 118| value | T | Yes| Target element.| 119 120**Return value** 121 122| Type| Description| 123| -------- | -------- | 124| boolean | Operation result. The value **true** is returned if the specified element is contained; otherwise, **false** is returned.| 125 126**Error codes** 127 128For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 129 130| ID| Error Message| 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**Example** 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 148Adds an element to this HashSet. 149 150**Atomic service API**: This API can be used in atomic services since API version 12. 151 152**System capability**: SystemCapability.Utils.Lang 153 154**Parameters** 155 156| Name| Type| Mandatory| Description| 157| -------- | -------- | -------- | -------- | 158| value | T | Yes| Target element.| 159 160**Return value** 161 162| Type| Description| 163| -------- | -------- | 164| boolean | Operation result. The value **true** is returned if the element is added; otherwise, **false** is returned.| 165 166**Error codes** 167 168For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 169 170| ID| Error Message| 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**Example** 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 187Removes an element from this HashSet. 188 189**Atomic service API**: This API can be used in atomic services since API version 12. 190 191**System capability**: SystemCapability.Utils.Lang 192 193**Parameters** 194 195| Name| Type| Mandatory| Description| 196| -------- | -------- | -------- | -------- | 197| value | T | Yes| Target element.| 198 199**Return value** 200 201| Type| Description| 202| -------- | -------- | 203| boolean | Operation result. The value **true** is returned if the element is removed; otherwise, **false** is returned.| 204 205**Error codes** 206 207For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 208 209| ID| Error Message| 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**Example** 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 228Clears this HashSet and sets its length to **0**. 229 230**Atomic service API**: This API can be used in atomic services since API version 12. 231 232**System capability**: SystemCapability.Utils.Lang 233 234**Error codes** 235 236For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 237 238| ID| Error Message| 239| -------- | -------- | 240| 10200011 | The clear method cannot be bound. | 241 242**Example** 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<T> 255 256Returns an iterator that contains all the values in this HashSet. 257 258**Atomic service API**: This API can be used in atomic services since API version 12. 259 260**System capability**: SystemCapability.Utils.Lang 261 262**Return value** 263 264| Type| Description| 265| -------- | -------- | 266| IterableIterator<T> | Iterator obtained.| 267 268**Error codes** 269 270For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 271 272| ID| Error Message| 273| -------- | -------- | 274| 10200011 | The values method cannot be bound. | 275 276**Example** 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<T>) => void, thisArg?: Object): void 294 295Uses a callback to traverse the elements in this HashSet and obtain their position indexes. 296 297**Atomic service API**: This API can be used in atomic services since API version 12. 298 299**System capability**: SystemCapability.Utils.Lang 300 301**Parameters** 302 303| Name| Type| Mandatory| Description| 304| -------- | -------- | -------- | -------- | 305| callbackFn | function | Yes| Callback invoked to traverse the elements in the HashSet.| 306| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 307 308callbackFn parameters 309| Name| Type| Mandatory| Description| 310| -------- | -------- | -------- | -------- | 311| value | T | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.| 312| 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.| 313| set | HashSet<T> | No| Instance that calls the **forEach** API. The default value is this instance.| 314 315**Error codes** 316 317For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 318 319| ID| Error Message| 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**Example** 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// 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. 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 348Returns an iterator that contains all the elements in this HashSet. 349 350**Atomic service API**: This API can be used in atomic services since API version 12. 351 352**System capability**: SystemCapability.Utils.Lang 353 354**Return value** 355 356| Type| Description| 357| -------- | -------- | 358| IterableIterator<[T, T]> | Iterator obtained.| 359 360**Error codes** 361 362For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 363 364| ID| Error Message| 365| -------- | -------- | 366| 10200011 | The entries method cannot be bound. | 367 368**Example** 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// 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. 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<T> 396 397Returns an iterator, each item of which is a JavaScript object. 398 399**Atomic service API**: This API can be used in atomic services since API version 12. 400 401**System capability**: SystemCapability.Utils.Lang 402 403**Return value** 404 405| Type| Description| 406| -------- | -------- | 407| IterableIterator<T> | Iterator obtained.| 408 409**Error codes** 410 411For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 412 413| ID| Error Message| 414| -------- | -------- | 415| 10200011 | The Symbol.iterator method cannot be bound. | 416 417**Example** 418 419```ts 420let hashSet: HashSet<string> = new HashSet(); 421hashSet.add("squirrel"); 422hashSet.add("sparrow"); 423 424// Method 1: 425let val: Array<string> = Array.from(hashSet.values()); 426for (let item of val) { 427 console.log("value: " + item); 428} 429 430// Method 2: 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// 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. 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``` 448