1# @ohos.util.TreeSet (非线性容器TreeSet) 2 3TreeSet基于[TreeMap](js-apis-treemap.md)实现,在TreeSet中,只对value对象进行处理。TreeSet可用于存储一系列值的集合,元素中value唯一且有序。 4 5TreeSet和[HashSet](js-apis-hashset.md)相比,HashSet中的数据无序存放,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不允许。 6 7**推荐使用场景:** 一般需要存储有序集合的场景,可以使用TreeSet。 8 9文档中存在泛型的使用,涉及以下泛型标记符: 10 11- T: Type, 类 12 13> **说明:** 14> 15> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 16 17## 导入模块 18 19```ts 20import TreeSet from '@ohos.util.TreeSet'; 21``` 22 23## TreeSet 24 25### 属性 26 27**系统能力:** SystemCapability.Utils.Lang 28 29| 名称 | 类型 | 可读 | 可写 | 说明 | 30| -------- | -------- | -------- | -------- | -------- | 31| length | number | 是 | 否 | TreeSet的元素个数。 | 32 33 34### constructor 35 36constructor(comparator?: (firstValue: T, secondValue: T) => boolean) 37 38TreeSet的构造函数。 39 40**系统能力:** SystemCapability.Utils.Lang 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| -------- | -------- | -------- | -------- | 46| comparator | function | 否 | 用户自定义的比较函数。 | 47 48**错误码:** 49 50以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 51 52| 错误码ID | 错误信息 | 53| -------- | -------- | 54| 10200012 | The TreeSet's constructor cannot be directly invoked. | 55 56**示例:** 57 58```ts 59let treeSet = new TreeSet(); 60``` 61 62 63### isEmpty 64 65isEmpty(): boolean 66 67判断该容器是否为空。 68 69**系统能力:** SystemCapability.Utils.Lang 70 71**返回值:** 72 73| 类型 | 说明 | 74| -------- | -------- | 75| boolean | 为空返回true,不为空返回false。 | 76 77**错误码:** 78 79以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 80 81| 错误码ID | 错误信息 | 82| -------- | -------- | 83| 10200011 | The isEmpty method cannot be bound. | 84 85**示例:** 86 87```ts 88const treeSet = new TreeSet(); 89let result = treeSet.isEmpty(); 90``` 91 92 93### has 94 95has(value: T): boolean 96 97判断此容器中是否含有该指定元素。 98 99**系统能力:** SystemCapability.Utils.Lang 100 101**参数:** 102 103| 参数名 | 类型 | 必填 | 说明 | 104| -------- | -------- | -------- | -------- | 105| value | T | 是 | 指定元素。 | 106 107**返回值:** 108 109| 类型 | 说明 | 110| -------- | -------- | 111| boolean | 包含指定元素返回true,否则返回false。 | 112 113**错误码:** 114 115以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 116 117| 错误码ID | 错误信息 | 118| -------- | -------- | 119| 10200011 | The has method cannot be bound. | 120 121**示例:** 122 123```ts 124let treeSet = new TreeSet(); 125treeSet.has(123); 126treeSet.add(123); 127let result1 = treeSet.has(123); 128``` 129 130 131### getFirstValue 132 133getFirstValue(): T 134 135获取容器中排序第一的数据。 136 137**系统能力:** SystemCapability.Utils.Lang 138 139**返回值:** 140 141| 类型 | 说明 | 142| -------- | -------- | 143| T | 返回排序第一的数据。 | 144 145**错误码:** 146 147以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 148 149| 错误码ID | 错误信息 | 150| -------- | -------- | 151| 10200011 | The getFirstValue method cannot be bound. | 152 153**示例:** 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 167获取容器中排序最后的数据。 168 169**系统能力:** SystemCapability.Utils.Lang 170 171**返回值:** 172 173| 类型 | 说明 | 174| -------- | -------- | 175| T | 返回排序最后的数据。 | 176 177**错误码:** 178 179以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 180 181| 错误码ID | 错误信息 | 182| -------- | -------- | 183| 10200011 | The getLastValue method cannot be bound. | 184 185**示例:** 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 199向容器中添加一组数据。 200 201**系统能力:** SystemCapability.Utils.Lang 202 203**参数:** 204 205| 参数名 | 类型 | 必填 | 说明 | 206| -------- | -------- | -------- | -------- | 207| value | T | 是 | 添加的成员数据。 | 208 209**返回值:** 210 211| 类型 | 说明 | 212| -------- | -------- | 213| boolean | 成功添加新数据至容器返回true,否则返回false。 | 214 215**错误码:** 216 217以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 218 219| 错误码ID | 错误信息 | 220| -------- | -------- | 221| 10200011 | The add method cannot be bound. | 222 223**示例:** 224 225```ts 226let treeSet = new TreeSet(); 227let result = treeSet.add("squirrel"); 228``` 229 230 231### remove 232 233remove(value: T): boolean 234 235删除指定的元素。 236 237**系统能力:** SystemCapability.Utils.Lang 238 239**参数:** 240 241| 参数名 | 类型 | 必填 | 说明 | 242| -------- | -------- | -------- | -------- | 243| value | T | 是 | 指定的元素。 | 244 245**返回值:** 246 247| 类型 | 说明 | 248| -------- | -------- | 249| boolean | 成功删除元素返回true,否则返回false。 | 250 251**错误码:** 252 253以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 254 255| 错误码ID | 错误信息 | 256| -------- | -------- | 257| 10200011 | The remove method cannot be bound. | 258 259**示例:** 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 273获取容器中比传入元素排序靠前一位的元素。 274 275**系统能力:** SystemCapability.Utils.Lang 276 277**参数:** 278 279| 参数名 | 类型 | 必填 | 说明 | 280| -------- | -------- | -------- | -------- | 281| key | T | 是 | 对比的元素值。 | 282 283**返回值:** 284 285| 类型 | 说明 | 286| -------- | -------- | 287| T | 返回排序中对比元素前一位的数据。 | 288 289**错误码:** 290 291以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 292 293| 错误码ID | 错误信息 | 294| -------- | -------- | 295| 10200011 | The getLowerValue method cannot be bound. | 296 297**示例:** 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 312获取容器中比传入元素排序靠后一位的元素。 313 314**系统能力:** SystemCapability.Utils.Lang 315 316**参数:** 317 318| 参数名 | 类型 | 必填 | 说明 | 319| -------- | -------- | -------- | -------- | 320| key | T | 是 | 对比的元素。 | 321 322**返回值:** 323 324| 类型 | 说明 | 325| -------- | -------- | 326| T | 返回排序中传入元素后一位的数据。 | 327 328**错误码:** 329 330以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 331 332| 错误码ID | 错误信息 | 333| -------- | -------- | 334| 10200011 | The getHigherValue method cannot be bound. | 335 336**示例:** 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 351删除容器中排序最前的数据。 352 353**系统能力:** SystemCapability.Utils.Lang 354 355**返回值:** 356 357| 类型 | 说明 | 358| -------- | -------- | 359| T | 返回删除的数据。 | 360 361**错误码:** 362 363以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 364 365| 错误码ID | 错误信息 | 366| -------- | -------- | 367| 10200011 | The popFirst method cannot be bound. | 368 369**示例:** 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 383删除容器中排序最后的数据。 384 385**系统能力:** SystemCapability.Utils.Lang 386 387**返回值:** 388 389| 类型 | 说明 | 390| -------- | -------- | 391| T | 返回删除的数据。 | 392 393**错误码:** 394 395以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 396 397| 错误码ID | 错误信息 | 398| -------- | -------- | 399| 10200011 | The popLast method cannot be bound. | 400 401**示例:** 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 415清除容器中的所有元素,并把length置为0。 416 417**系统能力:** SystemCapability.Utils.Lang 418 419**错误码:** 420 421以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 422 423| 错误码ID | 错误信息 | 424| -------- | -------- | 425| 10200011 | The clear method cannot be bound. | 426 427**示例:** 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 441返回包含此映射中键值的新迭代器对象。 442 443**系统能力:** SystemCapability.Utils.Lang 444 445**返回值:** 446 447| 类型 | 说明 | 448| -------- | -------- | 449| IterableIterator<T> | 返回一个迭代器。 | 450 451**错误码:** 452 453以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 454 455| 错误码ID | 错误信息 | 456| -------- | -------- | 457| 10200011 | The values method cannot be bound. | 458 459**示例:** 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 478通过回调函数来遍历实例对象上的元素以及元素对应的下标。 479 480**系统能力:** SystemCapability.Utils.Lang 481 482**参数:** 483 484| 参数名 | 类型 | 必填 | 说明 | 485| -------- | -------- | -------- | -------- | 486| callbackFn | function | 是 | 回调函数。 | 487| thisArg | Object | 否 | callbackFn被调用时用作this值。 | 488 489callbackFn的参数说明: 490| 参数名 | 类型 | 必填 | 说明 | 491| -------- | -------- | -------- | -------- | 492| value | T | 否 | 当前遍历到的value元素。 | 493| key | T | 否 | 当前遍历到的key元素。 | 494| set | TreeSet<T> | 否 | 当前调用forEach方法的实例对象。 | 495 496**错误码:** 497 498以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 499 500| 错误码ID | 错误信息 | 501| -------- | -------- | 502| 10200011 | The forEach method cannot be bound. | 503 504**示例:** 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 520返回包含此映射中键值对的新迭代器对象。 521 522**系统能力:** SystemCapability.Utils.Lang 523 524**返回值:** 525 526| 类型 | 说明 | 527| -------- | -------- | 528| IterableIterator<[T, T]> | 返回一个迭代器。 | 529 530**错误码:** 531 532以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 533 534| 错误码ID | 错误信息 | 535| -------- | -------- | 536| 10200011 | The entries method cannot be bound. | 537 538**示例:** 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 558返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 559 560**系统能力:** SystemCapability.Utils.Lang 561 562**返回值:** 563 564| 类型 | 说明 | 565| -------- | -------- | 566| IterableIterator<T> | 返回一个迭代器。 | 567 568**错误码:** 569 570以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 571 572| 错误码ID | 错误信息 | 573| -------- | -------- | 574| 10200011 | The Symbol.iterator method cannot be bound. | 575 576**示例:** 577 578```ts 579let treeSet = new TreeSet(); 580treeSet.add("squirrel"); 581treeSet.add("sparrow"); 582 583// 使用方法一: 584for (let item of treeSet) { 585 console.log("value:" + item); 586} 587 588// 使用方法二: 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```