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 20```ts 21import TreeSet from '@ohos.util.TreeSet'; 22``` 23 24## TreeSet 25 26### 属性 27 28**系统能力:** SystemCapability.Utils.Lang 29 30| 名称 | 类型 | 可读 | 可写 | 说明 | 31| -------- | -------- | -------- | -------- | -------- | 32| length | number | 是 | 否 | TreeSet的元素个数。 | 33 34 35### constructor 36 37constructor(comparator?: (firstValue: T, secondValue: T) => boolean) 38 39TreeSet的构造函数,支持通过比较函数对元素进行升序或降序排序。 40 41**系统能力:** SystemCapability.Utils.Lang 42 43**参数:** 44 45| 参数名 | 类型 | 必填 | 说明 | 46| -------- | -------- | -------- | -------- | 47| comparator | function | 否 | 用户自定义的比较函数,可通过比较关系对元素进行排序。默认值为hole(一个空白占位符),表示不提供比较函数。| 48 49**错误码:** 50 51以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 52 53| 错误码ID | 错误信息 | 54| -------- | -------- | 55| 10200012 | The TreeSet's constructor cannot be directly invoked. | 56 57**示例:** 58 59```ts 60//构造函数 61let treeSet : TreeSet<string | number | boolean | Object> = new TreeSet(); 62``` 63 64```ts 65//使用comparator firstValue < secondValue,表示期望结果为升序排序。反之firstValue > secondValue,表示为降序排序。 66let treeSet : TreeSet<string> = new TreeSet<string>((firstValue: string, secondValue: string) : boolean => {return firstValue < secondValue}); 67treeSet.add("a"); 68treeSet.add("c"); 69treeSet.add("d"); 70treeSet.add("b"); 71let numbers = Array.from(treeSet.values()) 72for (let item of numbers) { 73 console.log("TreeSet:" + item); 74} 75``` 76 77```ts 78//当插入自定义类型时,则必须要提供比较函数。 79class TestEntry{ 80 public id: number = 0; 81} 82let ts1: TreeSet<TestEntry> = new TreeSet<TestEntry>((t1: TestEntry, t2: TestEntry): boolean => {return t1.id > t2.id;}); 83let entry1: TestEntry = { 84 id: 0 85}; 86let entry2: TestEntry = { 87 id: 1 88} 89ts1.add(entry1); 90ts1.add(entry2); 91console.log("treeSet: ", ts1.length); 92``` 93 94### isEmpty 95 96isEmpty(): boolean 97 98判断该容器是否为空。 99 100**系统能力:** SystemCapability.Utils.Lang 101 102**返回值:** 103 104| 类型 | 说明 | 105| -------- | -------- | 106| boolean | 为空返回true,不为空返回false。 | 107 108**错误码:** 109 110以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 111 112| 错误码ID | 错误信息 | 113| -------- | -------- | 114| 10200011 | The isEmpty method cannot be bound. | 115 116**示例:** 117 118```ts 119const treeSet : TreeSet<string | number | boolean | Object> = new TreeSet(); 120let result = treeSet.isEmpty(); 121``` 122 123 124### has 125 126has(value: T): boolean 127 128判断此容器中是否含有该指定元素。 129 130**系统能力:** SystemCapability.Utils.Lang 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| -------- | -------- | -------- | -------- | 136| value | T | 是 | 指定元素。 | 137 138**返回值:** 139 140| 类型 | 说明 | 141| -------- | -------- | 142| boolean | 包含指定元素返回true,否则返回false。 | 143 144**错误码:** 145 146以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 147 148| 错误码ID | 错误信息 | 149| -------- | -------- | 150| 10200011 | The has method cannot be bound. | 151 152**示例:** 153 154```ts 155let treeSet : TreeSet<number> = new TreeSet(); 156treeSet.add(123); 157let result = treeSet.has(123); 158``` 159 160### getFirstValue 161 162getFirstValue(): T 163 164获取容器中排序第一的数据,为空时返回undefined。 165 166**系统能力:** SystemCapability.Utils.Lang 167 168**返回值:** 169 170| 类型 | 说明 | 171| -------- | -------- | 172| T | 返回排序第一的数据,为空时返回undefined。 | 173 174**错误码:** 175 176以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 177 178| 错误码ID | 错误信息 | 179| -------- | -------- | 180| 10200011 | The getFirstValue method cannot be bound. | 181 182**示例:** 183 184```ts 185let treeSet : TreeSet<string> = new TreeSet(); 186treeSet.add("squirrel"); 187treeSet.add("sparrow"); 188let result = treeSet.getFirstValue(); 189``` 190 191 192### getLastValue 193 194getLastValue(): T 195 196获取容器中排序最后的数据,为空时返回undefined。 197 198**系统能力:** SystemCapability.Utils.Lang 199 200**返回值:** 201 202| 类型 | 说明 | 203| -------- | -------- | 204| T | 返回排序最后的数据,为空时返回undefined。 | 205 206**错误码:** 207 208以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 209 210| 错误码ID | 错误信息 | 211| -------- | -------- | 212| 10200011 | The getLastValue method cannot be bound. | 213 214**示例:** 215 216```ts 217let treeSet : TreeSet<string> = new TreeSet(); 218treeSet.add("squirrel"); 219treeSet.add("sparrow"); 220let result = treeSet.getLastValue(); 221``` 222 223 224### add 225 226add(value: T): boolean 227 228向容器中添加一组数据。 229 230**系统能力:** SystemCapability.Utils.Lang 231 232**参数:** 233 234| 参数名 | 类型 | 必填 | 说明 | 235| -------- | -------- | -------- | -------- | 236| value | T | 是 | 添加的成员数据。 | 237 238**返回值:** 239 240| 类型 | 说明 | 241| -------- | -------- | 242| boolean | 成功添加新数据至容器返回true,否则返回false。 | 243 244**错误码:** 245 246以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 247 248| 错误码ID | 错误信息 | 249| -------- | -------- | 250| 10200011 | The add method cannot be bound. | 251 252**示例:** 253 254```ts 255let treeSet : TreeSet<string> = new TreeSet(); 256let result = treeSet.add("squirrel"); 257``` 258 259 260### remove 261 262remove(value: T): boolean 263 264删除指定的元素。 265 266**系统能力:** SystemCapability.Utils.Lang 267 268**参数:** 269 270| 参数名 | 类型 | 必填 | 说明 | 271| -------- | -------- | -------- | -------- | 272| value | T | 是 | 指定的元素。 | 273 274**返回值:** 275 276| 类型 | 说明 | 277| -------- | -------- | 278| boolean | 成功删除元素返回true,否则返回false。 | 279 280**错误码:** 281 282以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 283 284| 错误码ID | 错误信息 | 285| -------- | -------- | 286| 10200011 | The remove method cannot be bound. | 287 288**示例:** 289 290```ts 291let treeSet : TreeSet<string> = new TreeSet(); 292treeSet.add("squirrel"); 293treeSet.add("sparrow"); 294let result = treeSet.remove("sparrow"); 295``` 296 297 298### getLowerValue 299 300getLowerValue(key: T): T 301 302获取容器中比传入元素排序靠前一位的元素,为空时返回undefined。 303 304**系统能力:** SystemCapability.Utils.Lang 305 306**参数:** 307 308| 参数名 | 类型 | 必填 | 说明 | 309| -------- | -------- | -------- | -------- | 310| key | T | 是 | 对比的元素值。 | 311 312**返回值:** 313 314| 类型 | 说明 | 315| -------- | -------- | 316| T | 返回排序中对比元素前一位的数据,为空时返回undefined。 | 317 318**错误码:** 319 320以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 321 322| 错误码ID | 错误信息 | 323| -------- | -------- | 324| 10200011 | The getLowerValue method cannot be bound. | 325 326**示例:** 327 328```ts 329let treeSet : TreeSet<string> = new TreeSet(); 330treeSet.add("squirrel"); 331treeSet.add("sparrow"); 332treeSet.add("gander"); 333let result = treeSet.getLowerValue("sparrow"); 334``` 335 336 337### getHigherValue 338 339getHigherValue(key: T): T 340 341获取容器中比传入元素排序靠后一位的元素,为空时返回undefined。 342 343**系统能力:** SystemCapability.Utils.Lang 344 345**参数:** 346 347| 参数名 | 类型 | 必填 | 说明 | 348| -------- | -------- | -------- | -------- | 349| key | T | 是 | 对比的元素。 | 350 351**返回值:** 352 353| 类型 | 说明 | 354| -------- | -------- | 355| T | 返回排序中传入元素后一位的数据。为空时返回undefined。 | 356 357**错误码:** 358 359以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 360 361| 错误码ID | 错误信息 | 362| -------- | -------- | 363| 10200011 | The getHigherValue method cannot be bound. | 364 365**示例:** 366 367```ts 368let treeSet : TreeSet<string> = new TreeSet(); 369treeSet.add("squirrel"); 370treeSet.add("sparrow"); 371treeSet.add("gander"); 372let result = treeSet.getHigherValue("sparrow"); 373``` 374 375 376### popFirst 377 378popFirst(): T 379 380删除容器中排序最前的数据,为空时返回undefined。 381 382**系统能力:** SystemCapability.Utils.Lang 383 384**返回值:** 385 386| 类型 | 说明 | 387| -------- | -------- | 388| T | 返回删除的数据,为空时返回undefined。 | 389 390**错误码:** 391 392以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 393 394| 错误码ID | 错误信息 | 395| -------- | -------- | 396| 10200011 | The popFirst method cannot be bound. | 397 398**示例:** 399 400```ts 401let treeSet : TreeSet<string> = new TreeSet(); 402treeSet.add("squirrel"); 403treeSet.add("sparrow"); 404let result = treeSet.popFirst(); 405``` 406 407 408### popLast 409 410popLast(): T 411 412删除容器中排序最后的数据,为空时返回undefined。 413 414**系统能力:** SystemCapability.Utils.Lang 415 416**返回值:** 417 418| 类型 | 说明 | 419| -------- | -------- | 420| T | 返回删除的数据,为空时返回undefined。 | 421 422**错误码:** 423 424以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 425 426| 错误码ID | 错误信息 | 427| -------- | -------- | 428| 10200011 | The popLast method cannot be bound. | 429 430**示例:** 431 432```ts 433let treeSet : TreeSet<string> = new TreeSet(); 434treeSet.add("squirrel"); 435treeSet.add("sparrow"); 436let result = treeSet.popLast(); 437``` 438 439 440### clear 441 442clear(): void 443 444清除容器中的所有元素,并把length置为0。 445 446**系统能力:** SystemCapability.Utils.Lang 447 448**错误码:** 449 450以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 451 452| 错误码ID | 错误信息 | 453| -------- | -------- | 454| 10200011 | The clear method cannot be bound. | 455 456**示例:** 457 458```ts 459let treeSet : TreeSet<string> = new TreeSet(); 460treeSet.add("squirrel"); 461treeSet.add("sparrow"); 462treeSet.clear(); 463``` 464 465 466### values 467 468values(): IterableIterator<T> 469 470返回包含此映射中键值的新迭代器对象。 471 472**系统能力:** SystemCapability.Utils.Lang 473 474**返回值:** 475 476| 类型 | 说明 | 477| -------- | -------- | 478| IterableIterator<T> | 返回一个迭代器。 | 479 480**错误码:** 481 482以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 483 484| 错误码ID | 错误信息 | 485| -------- | -------- | 486| 10200011 | The values method cannot be bound. | 487 488**示例:** 489 490```ts 491let treeSet : TreeSet<string> = new TreeSet(); 492treeSet.add("squirrel"); 493treeSet.add("sparrow"); 494let it = treeSet.values(); 495let t: IteratorResult<string> = it.next(); 496while(!t.done) { 497 console.log("TreeSet: " + t.value); 498 t = it.next() 499} 500``` 501 502 503### forEach 504 505forEach(callbackFn: (value?: T, key?: T, set?: TreeSet<T>) => void, thisArg?: Object): void 506 507通过回调函数来遍历实例对象上的元素以及元素对应的下标。 508 509**系统能力:** SystemCapability.Utils.Lang 510 511**参数:** 512 513| 参数名 | 类型 | 必填 | 说明 | 514| -------- | -------- | -------- | -------- | 515| callbackFn | function | 是 | 回调函数。 | 516| thisArg | Object | 否 | callbackFn被调用时用作this值,默认值为当前实例对象。 | 517 518callbackFn的参数说明: 519| 参数名 | 类型 | 必填 | 说明 | 520| -------- | -------- | -------- | -------- | 521| value | T | 否 | 当前遍历到的value元素,默认值为首个键值对的值。 | 522| key | T | 否 | 当前遍历到的key元素,默认值为首个键值对的键。 | 523| set | TreeSet<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 | 524 525**错误码:** 526 527以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 528 529| 错误码ID | 错误信息 | 530| -------- | -------- | 531| 10200011 | The forEach method cannot be bound. | 532 533**示例:** 534 535```ts 536let treeSet : TreeSet<string> = new TreeSet(); 537treeSet.add("sparrow"); 538treeSet.add("gull"); 539treeSet.forEach((value ?: string, key ?: string) :void => { 540 console.log("value:" + value, "key:" + key); 541}); 542``` 543 544 545### entries 546 547entries(): IterableIterator<[T, T]> 548 549返回包含此映射中键值对的新迭代器对象。 550 551**系统能力:** SystemCapability.Utils.Lang 552 553**返回值:** 554 555| 类型 | 说明 | 556| -------- | -------- | 557| IterableIterator<[T, T]> | 返回一个迭代器。 | 558 559**错误码:** 560 561以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 562 563| 错误码ID | 错误信息 | 564| -------- | -------- | 565| 10200011 | The entries method cannot be bound. | 566 567**示例:** 568 569```ts 570let treeSet : TreeSet<string> = new TreeSet(); 571treeSet.add("squirrel"); 572treeSet.add("sparrow"); 573let it = treeSet.entries(); 574let t: IteratorResult<Object[]> = it.next(); 575while(!t.done) { 576 console.log("TreeSet: " + t.value); 577 t = it.next() 578} 579``` 580 581 582### [Symbol.iterator] 583 584[Symbol.iterator]\(): IterableIterator<T> 585 586返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 587 588> **说明:** 589> 590> 本接口不支持在.ets文件中使用 591 592**系统能力:** SystemCapability.Utils.Lang 593 594**返回值:** 595 596| 类型 | 说明 | 597| -------- | -------- | 598| IterableIterator<T> | 返回一个迭代器。 | 599 600**错误码:** 601 602以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 603 604| 错误码ID | 错误信息 | 605| -------- | -------- | 606| 10200011 | The Symbol.iterator method cannot be bound. | 607 608**示例:** 609 610```ts 611 612let treeSet : TreeSet<string> = new TreeSet(); 613treeSet.add("squirrel"); 614treeSet.add("sparrow"); 615let numbers = Array.from(treeSet.values()) 616// 使用方法一: 617for (let item of numbers) { 618 console.log("value:" + item); 619} 620// 使用方法二: 621let iter = treeSet[Symbol.iterator](); 622let temp: IteratorResult<string> = iter.next().value; 623while(temp != undefined) { 624 console.log("value:" + temp); 625 temp = iter.next().value; 626} 627```