1# @ohos.util.TreeSet (Nonlinear Container TreeSet) 2 3**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. 4 5**[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**. 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 '@ohos.util.TreeSet'; 22``` 23 24## TreeSet 25 26### Attributes 27 28**System capability**: SystemCapability.Utils.Lang 29 30| Name| Type| Readable| Writable| Description| 31| -------- | -------- | -------- | -------- | -------- | 32| length | number | Yes| No| Number of elements in a tree set (called container later).| 33 34 35### constructor 36 37constructor(comparator?: (firstValue: T, secondValue: T) => boolean) 38 39A constructor used to create a **TreeSet** instance. 40 41**System capability**: SystemCapability.Utils.Lang 42 43**Parameters** 44 45| Name| Type| Mandatory| Description| 46| -------- | -------- | -------- | -------- | 47| comparator | function | No| Custom comparator.| 48 49**Error codes** 50 51For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 52 53| ID| Error Message| 54| -------- | -------- | 55| 10200012 | The TreeSet's constructor cannot be directly invoked. | 56 57**Example** 58 59```ts 60let treeSet = new TreeSet(); 61``` 62 63 64### isEmpty 65 66isEmpty(): boolean 67 68Checks whether this container is empty (contains no element). 69 70**System capability**: SystemCapability.Utils.Lang 71 72**Return value** 73 74| Type| Description| 75| -------- | -------- | 76| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 77 78**Error codes** 79 80For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 81 82| ID| Error Message| 83| -------- | -------- | 84| 10200011 | The isEmpty method cannot be bound. | 85 86**Example** 87 88```ts 89const treeSet = new TreeSet(); 90let result = treeSet.isEmpty(); 91``` 92 93 94### has 95 96has(value: T): boolean 97 98Checks whether this container has the specified value. 99 100**System capability**: SystemCapability.Utils.Lang 101 102**Parameters** 103 104| Name| Type| Mandatory| Description| 105| -------- | -------- | -------- | -------- | 106| value | T | Yes| Target value.| 107 108**Return value** 109 110| Type| Description| 111| -------- | -------- | 112| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.| 113 114**Error codes** 115 116For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 117 118| ID| Error Message| 119| -------- | -------- | 120| 10200011 | The has method cannot be bound. | 121 122**Example** 123 124```ts 125let treeSet = new TreeSet(); 126treeSet.add(123); 127let result = treeSet.has(123); 128``` 129 130 131### getFirstValue 132 133getFirstValue(): T 134 135Obtains the value of the first element in this container. 136 137**System capability**: SystemCapability.Utils.Lang 138 139**Return value** 140 141| Type| Description| 142| -------- | -------- | 143| T | Value obtained.| 144 145**Error codes** 146 147For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 148 149| ID| Error Message| 150| -------- | -------- | 151| 10200011 | The getFirstValue method cannot be bound. | 152 153**Example** 154 155```ts 156let treeSet = new TreeSet(); 157treeSet.add("squirrel"); 158treeSet.add("sparrow"); 159let result = treeSet.getFirstValue(); 160``` 161 162 163### getLastValue 164 165getLastValue(): T 166 167Obtains the value of the last element in this container. 168 169**System capability**: SystemCapability.Utils.Lang 170 171**Return value** 172 173| Type| Description| 174| -------- | -------- | 175| T | Value obtained.| 176 177**Error codes** 178 179For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 180 181| ID| Error Message| 182| -------- | -------- | 183| 10200011 | The getLastValue method cannot be bound. | 184 185**Example** 186 187```ts 188let treeSet = new TreeSet(); 189treeSet.add("squirrel"); 190treeSet.add("sparrow"); 191let result = treeSet.getLastValue(); 192``` 193 194 195### add 196 197add(value: T): boolean 198 199Adds an element to this container. 200 201**System capability**: SystemCapability.Utils.Lang 202 203**Parameters** 204 205| Name| Type| Mandatory| Description| 206| -------- | -------- | -------- | -------- | 207| value | T | Yes| Target element.| 208 209**Return value** 210 211| Type| Description| 212| -------- | -------- | 213| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 214 215**Error codes** 216 217For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 218 219| ID| Error Message| 220| -------- | -------- | 221| 10200011 | The add method cannot be bound. | 222 223**Example** 224 225```ts 226let treeSet = new TreeSet(); 227let result = treeSet.add("squirrel"); 228``` 229 230 231### remove 232 233remove(value: T): boolean 234 235Removes the element with the specified key from this container. 236 237**System capability**: SystemCapability.Utils.Lang 238 239**Parameters** 240 241| Name| Type| Mandatory| Description| 242| -------- | -------- | -------- | -------- | 243| value | T | Yes| Key of the target element.| 244 245**Return value** 246 247| Type| Description| 248| -------- | -------- | 249| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 250 251**Error codes** 252 253For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 254 255| ID| Error Message| 256| -------- | -------- | 257| 10200011 | The remove method cannot be bound. | 258 259**Example** 260 261```ts 262let treeSet = new TreeSet(); 263treeSet.add("squirrel"); 264treeSet.add("sparrow"); 265let result = treeSet.remove("sparrow"); 266``` 267 268 269### getLowerValue 270 271getLowerValue(key: T): T 272 273Obtains the value that is placed in front of the input key in this container. 274 275**System capability**: SystemCapability.Utils.Lang 276 277**Parameters** 278 279| Name| Type| Mandatory| Description| 280| -------- | -------- | -------- | -------- | 281| key | T | Yes| Input key.| 282 283**Return value** 284 285| Type| Description| 286| -------- | -------- | 287| T | Value obtained.| 288 289**Error codes** 290 291For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 292 293| ID| Error Message| 294| -------- | -------- | 295| 10200011 | The getLowerValue method cannot be bound. | 296 297**Example** 298 299```ts 300let treeSet = new TreeSet(); 301treeSet.add("squirrel"); 302treeSet.add("sparrow"); 303treeSet.add("gander"); 304let result = treeSet.getLowerValue("sparrow"); 305``` 306 307 308### getHigherValue 309 310getHigherValue(key: T): T 311 312Obtains the value that is placed next to the input key in this container. 313 314**System capability**: SystemCapability.Utils.Lang 315 316**Parameters** 317 318| Name| Type| Mandatory| Description| 319| -------- | -------- | -------- | -------- | 320| key | T | Yes| Input key.| 321 322**Return value** 323 324| Type| Description| 325| -------- | -------- | 326| T | Value obtained.| 327 328**Error codes** 329 330For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 331 332| ID| Error Message| 333| -------- | -------- | 334| 10200011 | The getHigherValue method cannot be bound. | 335 336**Example** 337 338```ts 339let treeSet = new TreeSet(); 340treeSet.add("squirrel"); 341treeSet.add("sparrow"); 342treeSet.add("gander"); 343let result = treeSet.getHigherValue("sparrow"); 344``` 345 346 347### popFirst 348 349popFirst(): T 350 351Removes the first element in this container. 352 353**System capability**: SystemCapability.Utils.Lang 354 355**Return value** 356 357| Type| Description| 358| -------- | -------- | 359| T | Element removed.| 360 361**Error codes** 362 363For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 364 365| ID| Error Message| 366| -------- | -------- | 367| 10200011 | The popFirst method cannot be bound. | 368 369**Example** 370 371```ts 372let treeSet = new TreeSet(); 373treeSet.add("squirrel"); 374treeSet.add("sparrow"); 375let result = treeSet.popFirst(); 376``` 377 378 379### popLast 380 381popLast(): T 382 383Removes the last element in this container. 384 385**System capability**: SystemCapability.Utils.Lang 386 387**Return value** 388 389| Type| Description| 390| -------- | -------- | 391| T | Element removed.| 392 393**Error codes** 394 395For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 396 397| ID| Error Message| 398| -------- | -------- | 399| 10200011 | The popLast method cannot be bound. | 400 401**Example** 402 403```ts 404let treeSet = new TreeSet(); 405treeSet.add("squirrel"); 406treeSet.add("sparrow"); 407let result = treeSet.popLast(); 408``` 409 410 411### clear 412 413clear(): void 414 415Clears this container and sets its length to **0**. 416 417**System capability**: SystemCapability.Utils.Lang 418 419**Error codes** 420 421For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 422 423| ID| Error Message| 424| -------- | -------- | 425| 10200011 | The clear method cannot be bound. | 426 427**Example** 428 429```ts 430let treeSet = new TreeSet(); 431treeSet.add("squirrel"); 432treeSet.add("sparrow"); 433treeSet.clear(); 434``` 435 436 437### values 438 439values(): IterableIterator<T> 440 441Obtains an iterator that contains all the values in this container. 442 443**System capability**: SystemCapability.Utils.Lang 444 445**Return value** 446 447| Type| Description| 448| -------- | -------- | 449| IterableIterator<T> | Iterator obtained.| 450 451**Error codes** 452 453For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 454 455| ID| Error Message| 456| -------- | -------- | 457| 10200011 | The values method cannot be bound. | 458 459**Example** 460 461```ts 462let treeSet = new TreeSet(); 463treeSet.add("squirrel"); 464treeSet.add("sparrow"); 465let iter = treeSet.values(); 466let temp = iter.next().value; 467while(temp != undefined) { 468 console.log("value:" + temp); 469 temp = iter.next().value; 470} 471``` 472 473 474### forEach 475 476forEach(callbackFn: (value?: T, key?: T, set?: TreeSet<T>) => void, thisArg?: Object): void 477 478Uses a callback to traverse the elements in this container and obtain their position indexes. 479 480**System capability**: SystemCapability.Utils.Lang 481 482**Parameters** 483 484| Name| Type| Mandatory| Description| 485| -------- | -------- | -------- | -------- | 486| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 487| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked.| 488 489callbackfn 490| Name| Type| Mandatory| Description| 491| -------- | -------- | -------- | -------- | 492| value | T | No| Value of the element that is currently traversed.| 493| key | T | No| Key of the element that is currently traversed.| 494| set | TreeSet<T> | No| Instance that invokes the **forEach** method.| 495 496**Error codes** 497 498For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 499 500| ID| Error Message| 501| -------- | -------- | 502| 10200011 | The forEach method cannot be bound. | 503 504**Example** 505 506```ts 507let treeSet = new TreeSet(); 508treeSet.add("sparrow"); 509treeSet.add("gull"); 510treeSet.forEach((value, key) => { 511 console.log("value:" + value, "key:" + key); 512}); 513``` 514 515 516### entries 517 518entries(): IterableIterator<[T, T]> 519 520Obtains an iterator that contains all the elements in this container. 521 522**System capability**: SystemCapability.Utils.Lang 523 524**Return value** 525 526| Type| Description| 527| -------- | -------- | 528| IterableIterator<[T, T]> | Iterator obtained.| 529 530**Error codes** 531 532For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 533 534| ID| Error Message| 535| -------- | -------- | 536| 10200011 | The entries method cannot be bound. | 537 538**Example** 539 540```ts 541let treeSet = new TreeSet(); 542treeSet.add("squirrel"); 543treeSet.add("sparrow"); 544let iter = treeSet.entries(); 545let temp = iter.next().value; 546while(temp != undefined) { 547 console.log("key:" + temp[0]); 548 console.log("value:" + temp[1]); 549 temp = iter.next().value; 550} 551``` 552 553 554### [Symbol.iterator] 555 556[Symbol.iterator]\(): IterableIterator<T> 557 558Obtains an iterator, each item of which is a JavaScript object. 559 560**System capability**: SystemCapability.Utils.Lang 561 562**Return value** 563 564| Type| Description| 565| -------- | -------- | 566| IterableIterator<T> | Iterator obtained.| 567 568**Error codes** 569 570For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 571 572| ID| Error Message| 573| -------- | -------- | 574| 10200011 | The Symbol.iterator method cannot be bound. | 575 576**Example** 577 578```ts 579let treeSet = new TreeSet(); 580treeSet.add("squirrel"); 581treeSet.add("sparrow"); 582 583// Method 1: 584for (let item of treeSet) { 585 console.log("value:" + item); 586} 587 588// Method 2: 589let iter = treeSet[Symbol.iterator](); 590let temp = iter.next().value; 591while(temp != undefined) { 592 console.log("value:" + temp); 593 temp = iter.next().value; 594} 595``` 596