1# Nonlinear Container HashSet 2 3> **NOTE** 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. 6 7**HashSet** is implemented based on [HashMap](js-apis-hashmap.md). In **HashSet**, only the **value** object is processed. 8 9Unlike [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**. 10 11**Recommended use case**: Use **HashSet** when you need a set that has only unique elements or need to deduplicate a set. 12 13## Modules to Import 14 15```ts 16import HashSet from '@ohos.util.HashSet'; 17``` 18 19## HashSet 20 21### Attributes 22 23**System capability**: SystemCapability.Utils.Lang 24 25| Name| Type| Readable| Writable| Description| 26| -------- | -------- | -------- | -------- | -------- | 27| length | number | Yes| No| Number of elements in a hash set (called container later).| 28 29 30### constructor 31 32constructor() 33 34A constructor used to create a **HashSet** instance. 35 36**System capability**: SystemCapability.Utils.Lang 37 38**Example** 39 40```ts 41let hashSet = new HashSet(); 42``` 43 44 45### isEmpty 46 47isEmpty(): boolean 48 49Checks whether this container is empty (contains no element). 50 51**System capability**: SystemCapability.Utils.Lang 52 53**Return value** 54 55| Type| Description| 56| -------- | -------- | 57| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 58 59**Example** 60 61```ts 62const hashSet = new HashSet(); 63let result = hashSet.isEmpty(); 64``` 65 66 67### has 68 69has(value: T): boolean 70 71Checks whether this container contains the specified element. 72 73**System capability**: SystemCapability.Utils.Lang 74 75**Parameters** 76 77| Name| Type| Mandatory| Description| 78| -------- | -------- | -------- | -------- | 79| value | T | Yes| Target element.| 80 81**Return value** 82 83| Type| Description| 84| -------- | -------- | 85| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.| 86 87**Example** 88 89```ts 90let hashSet = new HashSet(); 91let result = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 92hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 93let result1 = hashSet.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 94``` 95 96 97### add 98 99add(value: T): boolean 100 101Adds an element to this container. 102 103**System capability**: SystemCapability.Utils.Lang 104 105**Parameters** 106 107| Name| Type| Mandatory| Description| 108| -------- | -------- | -------- | -------- | 109| value | T | Yes| Target element.| 110 111**Return value** 112 113| Type| Description| 114| -------- | -------- | 115| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 116 117**Example** 118 119```ts 120let hashSet = new HashSet(); 121let result = hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 122``` 123 124 125### remove 126 127remove(value: T): boolean 128 129Removes an element from this container. 130 131**System capability**: SystemCapability.Utils.Lang 132 133**Parameters** 134 135| Name| Type| Mandatory| Description| 136| -------- | -------- | -------- | -------- | 137| value | T | Yes| Target element.| 138 139**Return value** 140 141| Type| Description| 142| -------- | -------- | 143| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 144 145**Example** 146 147```ts 148let hashSet = new HashSet(); 149hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 150hashSet.add("sdfs"); 151let result = hashSet.remove("sdfs"); 152``` 153 154 155### clear 156 157clear(): void 158 159Clears this container and sets its length to **0**. 160 161**System capability**: SystemCapability.Utils.Lang 162 163**Example** 164 165```ts 166let hashSet = new HashSet(); 167hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 168hashSet.add("sdfs"); 169hashSet.clear(); 170``` 171 172 173### values 174 175values(): IterableIterator<T> 176 177Obtains an iterator that contains all the values in this container. 178 179**System capability**: SystemCapability.Utils.Lang 180 181**Return value** 182 183| Type| Description| 184| -------- | -------- | 185| IterableIterator<T> | Iterator obtained.| 186 187**Example** 188 189```ts 190let hashSet = new HashSet(); 191hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 192hashSet.add("sdfs"); 193let iter = hashSet.values(); 194let temp = iter.next().value; 195while(temp != undefined) { 196 console.log("value:" + temp); 197 temp = iter.next().value; 198} 199``` 200 201 202### forEach 203 204forEach(callbackfn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void 205 206Uses a callback to traverse the elements in this container and obtain their position indexes. 207 208**System capability**: SystemCapability.Utils.Lang 209 210**Parameters** 211 212| Name| Type| Mandatory| Description| 213| -------- | -------- | -------- | -------- | 214| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.| 215| thisArg | Object | No| Value to use when the callback is invoked.| 216 217callbackfn 218| Name| Type| Mandatory| Description| 219| -------- | -------- | -------- | -------- | 220| value | T | No| Value of the element that is currently traversed.| 221| key | T | No| Key of the element that is currently traversed (same as **value**).| 222| set | HashSet<T> | No| Instance that invokes the **forEach** method.| 223 224**Example** 225 226```ts 227let hashSet = new HashSet(); 228hashSet.add("sdfs"); 229hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 230hashSet.forEach((value, key) => { 231 console.log("value:" + value, "key:" + key); 232}); 233``` 234 235 236### entries 237entries(): IterableIterator<[T, T]> 238 239Obtains an iterator that contains all the elements in this container. 240 241**System capability**: SystemCapability.Utils.Lang 242 243**Return value** 244 245| Type| Description| 246| -------- | -------- | 247| IterableIterator<[T, T]> | Iterator obtained.| 248 249**Example** 250 251```ts 252let hashSet = new HashSet(); 253hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 254hashSet.add("sdfs"); 255let iter = hashSet.entries(); 256let temp = iter.next().value; 257while(temp != undefined) { 258 console.log("key:" + temp[0]); 259 console.log("value:" + temp[1]); 260 temp = iter.next().value; 261} 262``` 263 264 265### [Symbol.iterator] 266 267[Symbol.iterator]\(): IterableIterator<T> 268 269Obtains an iterator, each item of which is a JavaScript object. 270 271**System capability**: SystemCapability.Utils.Lang 272 273**Return value** 274 275| Type| Description| 276| -------- | -------- | 277| IterableIterator<T> | Iterator obtained.| 278 279**Example** 280 281```ts 282let hashSet = new HashSet(); 283hashSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 284hashSet.add("sdfs"); 285 286// Method 1: 287for (let item of hashSet) { 288 console.log("value: " + item); 289} 290 291// Method 2: 292let iter = hashSet[Symbol.iterator](); 293let temp = iter.next().value; 294while(temp != undefined) { 295 console.log("value: " + temp); 296 temp = iter.next().value; 297} 298``` 299