js-apis-treeset.md 12.5 KB
Newer Older
G
Gloria 已提交
1
# @ohos.util.TreeSet (Nonlinear Container TreeSet)
W
wusongqing 已提交
2

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
**TreeSet** is implemented based on **[TreeMap](js-apis-treemap.md)**. In **TreeSet**, only **value** objects are processed. **TreeSet** can be used to store values, each of which must be unique.

**[HashSet](js-apis-hashset.md)** stores data in a random order, whereas **TreeSet** stores data in sorted order. Both of them allows only unique elements. However, null values are allowed in **HashSet**, but not allowed in **TreeSet**.

Recommended use case: Use **TreeSet** when you need to store data in sorted order.
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
import TreeSet from '@ohos.util.TreeSet';  
W
wusongqing 已提交
20 21 22 23 24 25
```

## TreeSet

### 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 tree set (called container later).|
W
wusongqing 已提交
31 32 33 34


### constructor

G
Gloria 已提交
35
constructor(comparator?: (firstValue: T, secondValue: T) => boolean)
W
wusongqing 已提交
36 37 38

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

W
wusongqing 已提交
39 40
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
41 42 43 44 45 46
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Custom comparator.|

G
Gloria 已提交
47 48 49 50 51 52 53 54
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
55 56
**Example**

W
wusongqing 已提交
57
```ts
W
wusongqing 已提交
58 59 60 61 62 63 64 65
let treeSet = new TreeSet();
```


### isEmpty

isEmpty(): boolean

W
wusongqing 已提交
66 67 68
Checks whether this container is empty (contains no element).

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

**Return value**

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

G
Gloria 已提交
76 77 78 79 80 81 82 83
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
84 85
**Example**

W
wusongqing 已提交
86
```ts
W
wusongqing 已提交
87 88 89 90 91 92 93 94 95 96 97
const treeSet = new TreeSet();
let result = treeSet.isEmpty();
```


### has

has(value: T): boolean

Checks whether this container has the specified value.

W
wusongqing 已提交
98 99
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
100 101 102 103
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
104
| value | T | Yes| Target value.|
W
wusongqing 已提交
105 106 107 108 109 110 111

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|

G
Gloria 已提交
112 113 114 115 116 117 118 119
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
120 121
**Example**

W
wusongqing 已提交
122
```ts
W
wusongqing 已提交
123 124 125 126 127 128 129 130 131 132 133
let treeSet = new TreeSet();
treeSet.has(123);
treeSet.add(123);
let result1 = treeSet.has(123);
```


### getFirstValue

getFirstValue(): T

W
wusongqing 已提交
134 135 136
Obtains the value of the first element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
137 138 139 140 141 142 143

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value obtained.|

G
Gloria 已提交
144 145 146 147 148 149 150 151
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
152 153
**Example**

W
wusongqing 已提交
154
```ts
W
wusongqing 已提交
155
let treeSet = new TreeSet();
G
Gloria 已提交
156 157
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
158 159 160 161 162 163 164 165
let result = treeSet.getFirstValue();
```


### getLastValue

getLastValue(): T

W
wusongqing 已提交
166 167 168
Obtains the value of the last element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
169 170 171 172 173 174 175

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value obtained.|

G
Gloria 已提交
176 177 178 179 180 181 182 183
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
184 185
**Example**

W
wusongqing 已提交
186
```ts
W
wusongqing 已提交
187
let treeSet = new TreeSet();
G
Gloria 已提交
188 189
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
190 191 192 193 194
let result = treeSet.getLastValue();
```


### add
W
wusongqing 已提交
195

W
wusongqing 已提交
196 197
add(value: T): boolean

W
wusongqing 已提交
198 199 200
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
201 202 203 204 205

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
206
| value | T | Yes| Target element.|
W
wusongqing 已提交
207 208 209 210 211

**Return value**

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

G
Gloria 已提交
214 215 216 217 218 219 220 221
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
222 223
**Example**

W
wusongqing 已提交
224
```ts
W
wusongqing 已提交
225
let treeSet = new TreeSet();
G
Gloria 已提交
226
let result = treeSet.add("squirrel");
W
wusongqing 已提交
227 228 229 230 231
```


### remove

W
wusongqing 已提交
232
remove(value: T): boolean
W
wusongqing 已提交
233

W
wusongqing 已提交
234 235 236
Removes the element with the specified key from this container.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
242
| value | T | Yes| Key of the target element.|
W
wusongqing 已提交
243 244 245 246 247

**Return value**

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

G
Gloria 已提交
250 251 252 253 254 255 256 257
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
258 259
**Example**

W
wusongqing 已提交
260
```ts
W
wusongqing 已提交
261
let treeSet = new TreeSet();
G
Gloria 已提交
262 263 264
treeSet.add("squirrel");
treeSet.add("sparrow");
let result = treeSet.remove("sparrow");
W
wusongqing 已提交
265 266 267 268 269 270 271 272 273
```


### getLowerValue

getLowerValue(key: T): T

Obtains the value that is placed in front of the input key in this container.

W
wusongqing 已提交
274 275
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
276 277 278 279 280 281 282 283 284 285 286 287
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Input key.|

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value obtained.|

G
Gloria 已提交
288 289 290 291 292 293 294 295
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
296 297
**Example**

W
wusongqing 已提交
298
```ts
W
wusongqing 已提交
299
let treeSet = new TreeSet();
G
Gloria 已提交
300 301 302 303
treeSet.add("squirrel");
treeSet.add("sparrow");
treeSet.add("gander");
let result = treeSet.getLowerValue("sparrow");
W
wusongqing 已提交
304 305 306 307 308 309 310 311 312
```


### getHigherValue

getHigherValue(key: T): T

Obtains the value that is placed next to the input key in this container.

W
wusongqing 已提交
313 314
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
315 316 317 318 319 320 321 322 323 324 325 326
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | T | Yes| Input key.|

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value obtained.|

G
Gloria 已提交
327 328 329 330 331 332 333 334
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

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

W
wusongqing 已提交
337
```ts
W
wusongqing 已提交
338
let treeSet = new TreeSet();
G
Gloria 已提交
339 340 341 342
treeSet.add("squirrel");
treeSet.add("sparrow");
treeSet.add("gander");
let result = treeSet.getHigherValue("sparrow");
W
wusongqing 已提交
343 344 345 346 347 348 349
```


### popFirst

popFirst(): T

W
wusongqing 已提交
350 351 352
Removes the first element in this container.

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

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
358
| T | Element removed.|
W
wusongqing 已提交
359

G
Gloria 已提交
360 361 362 363 364 365 366 367
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
368
**Example**
W
wusongqing 已提交
369

W
wusongqing 已提交
370
```ts
W
wusongqing 已提交
371
let treeSet = new TreeSet();
G
Gloria 已提交
372 373
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
374 375 376 377 378 379 380 381
let result = treeSet.popFirst();
```


### popLast

popLast(): T

W
wusongqing 已提交
382 383 384
Removes the last element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
385 386 387 388 389

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
390
| T | Element removed.|
W
wusongqing 已提交
391

G
Gloria 已提交
392 393 394 395 396 397 398 399
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
400
**Example**
W
wusongqing 已提交
401

W
wusongqing 已提交
402
```ts
W
wusongqing 已提交
403
let treeSet = new TreeSet();
G
Gloria 已提交
404 405
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
406 407 408 409 410 411 412 413 414 415
let result = treeSet.popLast();
```


### clear

clear(): void

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

W
wusongqing 已提交
416 417
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
418 419 420 421 422 423 424 425
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
426 427
**Example**

W
wusongqing 已提交
428
```ts
W
wusongqing 已提交
429
let treeSet = new TreeSet();
G
Gloria 已提交
430 431
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
432 433 434 435 436 437 438 439 440 441
treeSet.clear();
```


### values

values(): IterableIterator<T>

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

W
wusongqing 已提交
442 443
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
444 445 446 447 448 449
**Return value**

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

G
Gloria 已提交
450 451 452 453 454 455 456 457
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
458 459
**Example**

W
wusongqing 已提交
460
```ts
W
wusongqing 已提交
461
let treeSet = new TreeSet();
G
Gloria 已提交
462 463
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
464 465 466
let iter = treeSet.values();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
467
  console.log("value:" + temp);
W
wusongqing 已提交
468
  temp = iter.next().value;
G
Gloria 已提交
469
}
W
wusongqing 已提交
470 471 472 473 474
```


### forEach

G
Gloria 已提交
475
forEach(callbackFn: (value?: T, key?: T, set?: TreeSet<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
476

W
wusongqing 已提交
477 478 479
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
480 481 482 483 484

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
485
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
486 487 488 489 490
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
491 492
| 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 已提交
493 494
| set | TreeSet<T> | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
495 496 497 498 499 500 501 502
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
503 504
**Example**

W
wusongqing 已提交
505
```ts
W
wusongqing 已提交
506
let treeSet = new TreeSet();
G
Gloria 已提交
507 508
treeSet.add("sparrow");
treeSet.add("gull");
W
wusongqing 已提交
509
treeSet.forEach((value, key) => {
W
wusongqing 已提交
510
  console.log("value:" + value, key)
W
wusongqing 已提交
511 512 513 514 515 516 517 518
});
```


### entries

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

W
wusongqing 已提交
519 520 521
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
522 523 524 525 526 527 528

**Return value**

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

G
Gloria 已提交
529 530 531 532 533 534 535 536
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
537 538
**Example**

W
wusongqing 已提交
539
```ts
W
wusongqing 已提交
540
let treeSet = new TreeSet();
G
Gloria 已提交
541 542
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
543 544 545
let iter = treeSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
546 547
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
548 549 550 551 552 553 554 555 556 557 558
  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 已提交
559 560
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
561 562 563 564 565 566
**Return value**

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

G
Gloria 已提交
567 568 569 570 571 572 573 574
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
575 576
**Example**

W
wusongqing 已提交
577
```ts
W
wusongqing 已提交
578
let treeSet = new TreeSet();
G
Gloria 已提交
579 580
treeSet.add("squirrel");
treeSet.add("sparrow");
W
wusongqing 已提交
581 582 583
  
// Method 1:
for (let item of treeSet) { 
W
wusongqing 已提交
584
  console.log("value:" + item);
W
wusongqing 已提交
585 586 587 588 589 590
}

// Method 2:
let iter = treeSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
591
  console.log("value:" + temp);
W
wusongqing 已提交
592 593 594
  temp = iter.next().value;
}
```