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