1# @ohos.util.TreeSet (Nonlinear Container TreeSet) 2 3TreeSet 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. 4 5TreeSet and [HashSet](js-apis-hashset.md) do not allow duplicate elements. Data in a HashSet is stored in an unordered manner, whereas elements in a TreeSet are stored in a sorted order. HashSet permits the insertion of null values. However, you are not advised to insert null into a TreeSet, as it may affect the sorting results. 6 7Recommended use case: Use TreeSet when you need to store data in sorted order. 8 9This topic uses the following to identify the use of generics: 10 11- T: Type 12 13> **NOTE** 14> 15> 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. 16 17 18## Modules to Import 19 20```ts 21import { TreeSet } from '@kit.ArkTS'; 22``` 23 24## TreeSet 25 26### Properties 27 28**Atomic service API**: This API can be used in atomic services since API version 12. 29 30**System capability**: SystemCapability.Utils.Lang 31 32| Name| Type| Readable| Writable| Description| 33| -------- | -------- | -------- | -------- | -------- | 34| length | number | Yes| No| Number of elements in a TreeSet.| 35 36 37### constructor 38 39constructor(comparator?: (firstValue: T, secondValue: T) => boolean) 40 41A constructor used to create a **TreeSet** instance. It supports sorting elements in ascending or descending order by using comparators. 42 43**Atomic service API**: This API can be used in atomic services since API version 12. 44 45**System capability**: SystemCapability.Utils.Lang 46 47**Parameters** 48 49| Name| Type| Mandatory| Description| 50| -------- | -------- | -------- | -------- | 51| comparator | function | No| Custom comparator, which can be used to sort elements based on the comparison relationship. The default value is **hole** (a blank placeholder), indicating that no comparator is provided.| 52 53comparator parameters 54 55| Name| Type| Mandatory| Description| 56| ------| ---- | ----- | -- | 57| firstValue | T | Yes| Previous element.| 58| secondValue | T | Yes| Next element.| 59 60**Error codes** 61 62For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 63 64| ID| Error Message| 65| -------- | -------- | 66| 401 | Parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 67| 10200012 | The TreeSet's constructor cannot be directly invoked. | 68 69**Example** 70 71```ts 72// Default constructor. 73let treeSet : TreeSet<string | number | boolean | Object> = new TreeSet(); 74``` 75 76```ts 77// Use the comparator firstValue < secondValue if the elements are expected to be sorted in ascending order. Use firstValue > secondValue if the elements are expected to be sorted in descending order. 78let treeSet : TreeSet<string> = new TreeSet<string>((firstValue: string, secondValue: string) : boolean => {return firstValue < secondValue}); 79treeSet.add("a"); 80treeSet.add("c"); 81treeSet.add("d"); 82treeSet.add("b"); 83let numbers = Array.from(treeSet.values()); 84for (let item of numbers) { 85 console.log("TreeSet:" + item); 86} 87``` 88 89```ts 90// When a custom type is inserted, a comparator must be provided. 91class TestEntry{ 92 public id: number = 0; 93} 94let ts1: TreeSet<TestEntry> = new TreeSet<TestEntry>((t1: TestEntry, t2: TestEntry): boolean => {return t1.id > t2.id;}); 95let entry1: TestEntry = { 96 id: 0 97}; 98let entry2: TestEntry = { 99 id: 1 100} 101ts1.add(entry1); 102ts1.add(entry2); 103console.log("treeSet: ", ts1.length); 104``` 105 106 107### isEmpty 108 109isEmpty(): boolean 110 111Checks whether this TreeSet is empty (contains no element). 112 113**Atomic service API**: This API can be used in atomic services since API version 12. 114 115**System capability**: SystemCapability.Utils.Lang 116 117**Return value** 118 119| Type| Description| 120| -------- | -------- | 121| boolean | Returns **true** if the TreeSet is empty; returns **false** otherwise.| 122 123**Error codes** 124 125For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 126 127| ID| Error Message| 128| -------- | -------- | 129| 10200011 | The isEmpty method cannot be bound. | 130 131**Example** 132 133```ts 134const treeSet : TreeSet<string | number | boolean | Object> = new TreeSet(); 135let result = treeSet.isEmpty(); 136``` 137 138 139### has 140 141has(value: T): boolean 142 143Checks whether this TreeSet has the specified value. 144 145**Atomic service API**: This API can be used in atomic services since API version 12. 146 147**System capability**: SystemCapability.Utils.Lang 148 149**Parameters** 150 151| Name| Type| Mandatory| Description| 152| -------- | -------- | -------- | -------- | 153| value | T | Yes| Target value.| 154 155**Return value** 156 157| Type| Description| 158| -------- | -------- | 159| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.| 160 161**Error codes** 162 163For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 164 165| ID| Error Message| 166| -------- | -------- | 167| 10200011 | The has method cannot be bound. | 168 169**Example** 170 171```ts 172let treeSet : TreeSet<number> = new TreeSet(); 173treeSet.add(123); 174let result = treeSet.has(123); 175``` 176 177### getFirstValue 178 179getFirstValue(): T 180 181Obtains the value of the first element in this TreeSet. 182 183**Atomic service API**: This API can be used in atomic services since API version 12. 184 185**System capability**: SystemCapability.Utils.Lang 186 187**Return value** 188 189| Type| Description| 190| -------- | -------- | 191| T | Value obtained. If nothing is obtained, **undefined** is returned.| 192 193**Error codes** 194 195For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 196 197| ID| Error Message| 198| -------- | -------- | 199| 10200011 | The getFirstValue method cannot be bound. | 200 201**Example** 202 203```ts 204let treeSet : TreeSet<string> = new TreeSet(); 205treeSet.add("squirrel"); 206treeSet.add("sparrow"); 207let result = treeSet.getFirstValue(); 208``` 209 210 211### getLastValue 212 213getLastValue(): T 214 215Obtains the value of the last element in this TreeSet. 216 217**Atomic service API**: This API can be used in atomic services since API version 12. 218 219**System capability**: SystemCapability.Utils.Lang 220 221**Return value** 222 223| Type| Description| 224| -------- | -------- | 225| T | Value obtained. If nothing is obtained, **undefined** is returned.| 226 227**Error codes** 228 229For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 230 231| ID| Error Message| 232| -------- | -------- | 233| 10200011 | The getLastValue method cannot be bound. | 234 235**Example** 236 237```ts 238let treeSet : TreeSet<string> = new TreeSet(); 239treeSet.add("squirrel"); 240treeSet.add("sparrow"); 241let result = treeSet.getLastValue(); 242``` 243 244 245### add 246 247add(value: T): boolean 248 249Adds an element to this TreeSet. 250 251**Atomic service API**: This API can be used in atomic services since API version 12. 252 253**System capability**: SystemCapability.Utils.Lang 254 255**Parameters** 256 257| Name| Type| Mandatory| Description| 258| -------- | -------- | -------- | -------- | 259| value | T | Yes| Target element.| 260 261**Return value** 262 263| Type| Description| 264| -------- | -------- | 265| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 266 267**Error codes** 268 269For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 270 271| ID| Error Message| 272| -------- | -------- | 273| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 274| 10200011 | The add method cannot be bound. | 275 276**Example** 277 278```ts 279let treeSet : TreeSet<string> = new TreeSet(); 280let result = treeSet.add("squirrel"); 281``` 282 283 284### remove 285 286remove(value: T): boolean 287 288Removes the element with the specified key from this TreeSet. 289 290**Atomic service API**: This API can be used in atomic services since API version 12. 291 292**System capability**: SystemCapability.Utils.Lang 293 294**Parameters** 295 296| Name| Type| Mandatory| Description| 297| -------- | -------- | -------- | -------- | 298| value | T | Yes| Key of the target element.| 299 300**Return value** 301 302| Type| Description| 303| -------- | -------- | 304| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 305 306**Error codes** 307 308For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 309 310| ID| Error Message| 311| -------- | -------- | 312| 10200011 | The remove method cannot be bound. | 313 314**Example** 315 316```ts 317let treeSet : TreeSet<string> = new TreeSet(); 318treeSet.add("squirrel"); 319treeSet.add("sparrow"); 320let result = treeSet.remove("sparrow"); 321``` 322 323 324### getLowerValue 325 326getLowerValue(key: T): T 327 328Obtains the value that is placed in front of the input key in this TreeSet. 329 330**Atomic service API**: This API can be used in atomic services since API version 12. 331 332**System capability**: SystemCapability.Utils.Lang 333 334**Parameters** 335 336| Name| Type| Mandatory| Description| 337| -------- | -------- | -------- | -------- | 338| key | T | Yes| Input key.| 339 340**Return value** 341 342| Type| Description| 343| -------- | -------- | 344| T | Value obtained. If nothing is obtained, **undefined** is returned.| 345 346**Error codes** 347 348For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 349 350| ID| Error Message| 351| -------- | -------- | 352| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 353| 10200011 | The getLowerValue method cannot be bound. | 354 355**Example** 356 357```ts 358let treeSet : TreeSet<string> = new TreeSet(); 359treeSet.add("squirrel"); 360treeSet.add("sparrow"); 361treeSet.add("gander"); 362let result = treeSet.getLowerValue("sparrow"); 363``` 364 365 366### getHigherValue 367 368getHigherValue(key: T): T 369 370Obtains the value that is placed next to the input key in this TreeSet. 371 372**Atomic service API**: This API can be used in atomic services since API version 12. 373 374**System capability**: SystemCapability.Utils.Lang 375 376**Parameters** 377 378| Name| Type| Mandatory| Description| 379| -------- | -------- | -------- | -------- | 380| key | T | Yes| Input key.| 381 382**Return value** 383 384| Type| Description| 385| -------- | -------- | 386| T | Value obtained. If nothing is obtained, **undefined** is returned.| 387 388**Error codes** 389 390For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 391 392| ID| Error Message| 393| -------- | -------- | 394| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 395| 10200011 | The getHigherValue method cannot be bound. | 396 397**Example** 398 399```ts 400let treeSet : TreeSet<string> = new TreeSet(); 401treeSet.add("squirrel"); 402treeSet.add("sparrow"); 403treeSet.add("gander"); 404let result = treeSet.getHigherValue("sparrow"); 405``` 406 407 408### popFirst 409 410popFirst(): T 411 412Removes the first element in this TreeSet. 413 414**Atomic service API**: This API can be used in atomic services since API version 12. 415 416**System capability**: SystemCapability.Utils.Lang 417 418**Return value** 419 420| Type| Description| 421| -------- | -------- | 422| T | Element removed. If nothing is obtained, **undefined** is returned.| 423 424**Error codes** 425 426For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 427 428| ID| Error Message| 429| -------- | -------- | 430| 10200011 | The popFirst method cannot be bound. | 431 432**Example** 433 434```ts 435let treeSet : TreeSet<string> = new TreeSet(); 436treeSet.add("squirrel"); 437treeSet.add("sparrow"); 438let result = treeSet.popFirst(); 439``` 440 441 442### popLast 443 444popLast(): T 445 446Removes the last element in this TreeSet. 447 448**Atomic service API**: This API can be used in atomic services since API version 12. 449 450**System capability**: SystemCapability.Utils.Lang 451 452**Return value** 453 454| Type| Description| 455| -------- | -------- | 456| T | Element removed. If nothing is obtained, **undefined** is returned.| 457 458**Error codes** 459 460For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 461 462| ID| Error Message| 463| -------- | -------- | 464| 10200011 | The popLast method cannot be bound. | 465 466**Example** 467 468```ts 469let treeSet : TreeSet<string> = new TreeSet(); 470treeSet.add("squirrel"); 471treeSet.add("sparrow"); 472let result = treeSet.popLast(); 473``` 474 475 476### clear 477 478clear(): void 479 480Clears this TreeSet and sets its length to **0**. 481 482**Atomic service API**: This API can be used in atomic services since API version 12. 483 484**System capability**: SystemCapability.Utils.Lang 485 486**Error codes** 487 488For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 489 490| ID| Error Message| 491| -------- | -------- | 492| 10200011 | The clear method cannot be bound. | 493 494**Example** 495 496```ts 497let treeSet : TreeSet<string> = new TreeSet(); 498treeSet.add("squirrel"); 499treeSet.add("sparrow"); 500treeSet.clear(); 501``` 502 503 504### values 505 506values(): IterableIterator<T> 507 508Returns an iterator that contains all the values in this TreeSet. 509 510**Atomic service API**: This API can be used in atomic services since API version 12. 511 512**System capability**: SystemCapability.Utils.Lang 513 514**Return value** 515 516| Type| Description| 517| -------- | -------- | 518| IterableIterator<T> | Iterator obtained.| 519 520**Error codes** 521 522For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 523 524| ID| Error Message| 525| -------- | -------- | 526| 10200011 | The values method cannot be bound. | 527 528**Example** 529 530```ts 531let treeSet : TreeSet<string> = new TreeSet(); 532treeSet.add("squirrel"); 533treeSet.add("sparrow"); 534let it = treeSet.values(); 535let t: IteratorResult<string> = it.next(); 536while(!t.done) { 537 console.log("TreeSet: " + t.value); 538 t = it.next() 539} 540``` 541 542 543### forEach 544 545forEach(callbackFn: (value?: T, key?: T, set?: TreeSet<T>) => void, thisArg?: Object): void 546 547Uses a callback to traverse the elements in this TreeSet and obtain their indexes. 548 549**Atomic service API**: This API can be used in atomic services since API version 12. 550 551**System capability**: SystemCapability.Utils.Lang 552 553**Parameters** 554 555| Name| Type| Mandatory| Description| 556| -------- | -------- | -------- | -------- | 557| callbackFn | function | Yes| Callback invoked to traverse the elements in the TreeSet.| 558| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 559 560callbackFn 561| Name| Type| Mandatory| Description| 562| -------- | -------- | -------- | -------- | 563| value | T | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.| 564| key | T | No| Key of the element that is currently traversed. The default value is the key of the first key-value pair.| 565| set | TreeSet<T> | No| Instance that calls the **forEach** API. The default value is this instance.| 566 567**Error codes** 568 569For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 570 571| ID| Error Message| 572| -------- | -------- | 573| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 574| 10200011 | The forEach method cannot be bound. | 575 576**Example** 577 578```ts 579let treeSet : TreeSet<string> = new TreeSet(); 580treeSet.add("sparrow"); 581treeSet.add("gull"); 582treeSet.forEach((value ?: string, key ?: string) :void => { 583 console.log("value:" + value, "key:" + key); 584}); 585``` 586```ts 587// 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. 588let treeSet : TreeSet<string> = new TreeSet(); 589for(let i = 0; i < 10; i++) { 590 treeSet.add("sparrow" + i); 591} 592for(let i = 0; i < 10; i++) { 593 treeSet.remove("sparrow" + i); 594} 595``` 596 597### entries 598 599entries(): IterableIterator<[T, T]> 600 601Returns an iterator that contains all the elements in this TreeSet. 602 603**Atomic service API**: This API can be used in atomic services since API version 12. 604 605**System capability**: SystemCapability.Utils.Lang 606 607**Return value** 608 609| Type| Description| 610| -------- | -------- | 611| IterableIterator<[T, T]> | Iterator obtained.| 612 613**Error codes** 614 615For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 616 617| ID| Error Message| 618| -------- | -------- | 619| 10200011 | The entries method cannot be bound. | 620 621**Example** 622 623```ts 624let treeSet : TreeSet<string> = new TreeSet(); 625treeSet.add("squirrel"); 626treeSet.add("sparrow"); 627let it = treeSet.entries(); 628let t: IteratorResult<Object[]> = it.next(); 629while(!t.done) { 630 console.log("TreeSet: " + t.value); 631 t = it.next() 632} 633``` 634```ts 635// 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. 636let treeSet : TreeSet<string> = new TreeSet(); 637for(let i = 0; i < 10; i++) { 638 treeSet.add("sparrow" + i); 639} 640for(let i = 0; i < 10; i++) { 641 treeSet.remove("sparrow" + i); 642} 643``` 644 645### [Symbol.iterator] 646 647[Symbol.iterator]\(): IterableIterator<T> 648 649Returns an iterator, each item of which is a JavaScript object. 650 651**Atomic service API**: This API can be used in atomic services since API version 12. 652 653**System capability**: SystemCapability.Utils.Lang 654 655**Return value** 656 657| Type| Description| 658| -------- | -------- | 659| IterableIterator<T> | Iterator obtained.| 660 661**Error codes** 662 663For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 664 665| ID| Error Message| 666| -------- | -------- | 667| 10200011 | The Symbol.iterator method cannot be bound. | 668 669**Example** 670 671```ts 672let treeSet : TreeSet<string> = new TreeSet(); 673treeSet.add("squirrel"); 674treeSet.add("sparrow"); 675let numbers = Array.from(treeSet.values()); 676// Method 1: 677for (let item of numbers) { 678 console.log("value:" + item); 679} 680// Method 2: 681let iter = treeSet[Symbol.iterator](); 682let temp: IteratorResult<string> = iter.next().value; 683while(temp != undefined) { 684 console.log("value:" + temp); 685 temp = iter.next().value; 686} 687``` 688```ts 689// 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. 690let treeSet : TreeSet<string> = new TreeSet(); 691for(let i = 0; i < 10; i++) { 692 treeSet.add("sparrow" + i); 693} 694for(let i = 0; i < 10; i++) { 695 treeSet.remove("sparrow" + i); 696} 697``` 698