js-apis-hashset.md 6.5 KB
Newer Older
W
wusongqing 已提交
1 2
# Nonlinear Container HashSet

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6
> 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 已提交
7 8 9 10 11
**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 已提交
12

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

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

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

## HashSet

### Attributes

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

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

G
Gloria 已提交
32 33 34 35 36 37 38 39 40 41 42
**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 已提交
43 44 45 46 47 48 49

### constructor

constructor()

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

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

W
wusongqing 已提交
52 53
**Example**

W
wusongqing 已提交
54
```ts
W
wusongqing 已提交
55 56 57 58 59 60 61 62
let hashSet = new HashSet();
```


### isEmpty

isEmpty(): boolean

W
wusongqing 已提交
63 64 65
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
66 67 68 69 70 71 72 73 74

**Return value**

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

**Example**

W
wusongqing 已提交
75
```ts
W
wusongqing 已提交
76
const hashSet = new HashSet();
W
wusongqing 已提交
77
let result = hashSet.isEmpty();
W
wusongqing 已提交
78 79 80 81 82 83 84
```


### has

has(value: T): boolean

W
wusongqing 已提交
85 86 87
Checks whether this container contains the specified element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
88 89 90 91 92

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
93
| value | T | Yes| Target element.|
W
wusongqing 已提交
94 95 96 97 98

**Return value**

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

**Example**

W
wusongqing 已提交
103
```ts
W
wusongqing 已提交
104
let hashSet = new HashSet();
G
Gloria 已提交
105 106 107
let result = hashSet.has("squirrel");
hashSet.add("squirrel");
let result1 = hashSet.has("squirrel");
W
wusongqing 已提交
108 109 110 111 112 113 114
```


### add

add(value: T): boolean

W
wusongqing 已提交
115 116 117
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
118 119 120 121 122

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
123
| value | T | Yes| Target element.|
W
wusongqing 已提交
124 125 126 127 128

**Return value**

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

**Example**

W
wusongqing 已提交
133
```ts
W
wusongqing 已提交
134
let hashSet = new HashSet();
G
Gloria 已提交
135
let result = hashSet.add("squirrel");
W
wusongqing 已提交
136 137 138 139 140 141 142
```


### remove

remove(value: T): boolean

W
wusongqing 已提交
143 144 145
Removes an element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
146 147 148 149 150

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
151
| value | T | Yes| Target element.|
W
wusongqing 已提交
152 153 154 155 156

**Return value**

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

**Example**

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


### clear

clear(): void

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

W
wusongqing 已提交
175 176
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
177 178
**Example**

W
wusongqing 已提交
179
```ts
W
wusongqing 已提交
180
let hashSet = new HashSet();
G
Gloria 已提交
181 182
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
183 184 185 186 187 188 189 190 191 192
hashSet.clear();
```


### values

values(): IterableIterator<T>

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

W
wusongqing 已提交
193 194
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
195 196 197 198 199 200 201 202
**Return value**

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

**Example**

W
wusongqing 已提交
203
```ts
W
wusongqing 已提交
204
let hashSet = new HashSet();
G
Gloria 已提交
205 206
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
207 208 209
let iter = hashSet.values();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
210
  console.log("value:" + temp);
W
wusongqing 已提交
211 212 213 214 215 216 217
  temp = iter.next().value;
} 
```


### forEach

W
wusongqing 已提交
218
forEach(callbackfn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
219

W
wusongqing 已提交
220 221 222
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
223 224 225 226 227

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
228
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
229 230 231 232 233
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
234 235
| 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**).|
W
wusongqing 已提交
236 237 238 239
| set | HashSet<T> | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
240
```ts
W
wusongqing 已提交
241
let hashSet = new HashSet();
G
Gloria 已提交
242 243
hashSet.add("sparrow");
hashSet.add("squirrel");
W
wusongqing 已提交
244
hashSet.forEach((value, key) => {
W
wusongqing 已提交
245
  console.log("value:" + value, key);
W
wusongqing 已提交
246 247 248 249 250 251 252
});
```


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

W
wusongqing 已提交
253 254 255
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
256 257 258 259 260 261 262 263 264

**Return value**

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

**Example**

W
wusongqing 已提交
265
```ts
W
wusongqing 已提交
266
let hashSet = new HashSet();
G
Gloria 已提交
267 268
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
269 270 271
let iter = hashSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
272 273
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
274 275 276 277 278 279 280 281 282 283 284
  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 已提交
285 286
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
287 288 289 290 291 292 293 294
**Return value**

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

**Example**

W
wusongqing 已提交
295
```ts
W
wusongqing 已提交
296
let hashSet = new HashSet();
G
Gloria 已提交
297 298
hashSet.add("squirrel");
hashSet.add("sparrow");
W
wusongqing 已提交
299 300 301 302 303 304 305 306 307 308

// 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 已提交
309
  console.log("value: " + temp);
W
wusongqing 已提交
310 311 312
  temp = iter.next().value;
}
```