js-apis-treeset.md 13.0 KB
Newer Older
1
# @ohos.util.TreeSet (非线性容器TreeSet)
L
linhaoran 已提交
2 3 4 5

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

6 7 8 9 10
TreeSet基于[TreeMap](js-apis-treemap.md)实现,在TreeSet中,只对value对象进行处理。TreeSet可用于存储一系列值的集合,元素中value唯一且有序。

TreeSet和[HashSet](js-apis-hashset.md)相比,HashSet中的数据无序存放,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不允许。

**推荐使用场景:** 一般需要存储有序集合的场景,可以使用TreeSet。
L
linhaoran 已提交
11

L
lengchangjing 已提交
12 13 14
文档中存在泛型的使用,涉及以下泛型标记符:<br>
- T: Type, 类

L
linhaoran 已提交
15 16
## 导入模块

17
```ts
18
import TreeSet from '@ohos.util.TreeSet';  
L
linhaoran 已提交
19 20 21 22 23 24
```

## TreeSet

### 属性

Z
zengyawen 已提交
25 26
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
27
| 名称 | 类型 | 可读 | 可写 | 说明 |
L
linhaoran 已提交
28
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
29
| length | number | 是 | 否 | TreeSet的元素个数。 |
L
linhaoran 已提交
30 31 32 33


### constructor

L
lengchangjing 已提交
34
constructor(comparator?: (firstValue: T, secondValue: T) => boolean)
L
linhaoran 已提交
35 36 37

TreeSet的构造函数。

Z
zengyawen 已提交
38 39
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
40 41 42 43 44 45
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| comparator | function | 否 | 用户自定义的比较函数。 |

L
liu-ganlin 已提交
46 47 48 49
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

L
liu-ganlin 已提交
50
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
51 52 53
| -------- | -------- |
| 10200012 | The TreeSet's constructor cannot be directly invoked. |

Z
zengyawen 已提交
54
**示例:**
L
linhaoran 已提交
55

56
```ts
Z
zengyawen 已提交
57 58
let treeSet = new TreeSet();
```
L
linhaoran 已提交
59 60 61 62


### isEmpty

Z
zengyawen 已提交
63
isEmpty(): boolean
L
linhaoran 已提交
64 65 66

判断该容器是否为空。

Z
zengyawen 已提交
67 68
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
69
**返回值:**
L
linhaoran 已提交
70

Z
zengyawen 已提交
71 72 73 74
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |

L
liu-ganlin 已提交
75 76 77 78 79 80 81 82
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

Z
zengyawen 已提交
83 84
**示例:**

85
```ts
Z
zengyawen 已提交
86
const treeSet = new TreeSet();
87
let result = treeSet.isEmpty();
Z
zengyawen 已提交
88
```
L
linhaoran 已提交
89 90 91 92


### has

Z
zengyawen 已提交
93
has(value: T): boolean
L
linhaoran 已提交
94

95
判断此容器中是否含有该指定元素。
L
linhaoran 已提交
96

Z
zengyawen 已提交
97 98
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
99 100 101 102 103
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 指定元素。 |
L
linhaoran 已提交
104

Z
zengyawen 已提交
105
**返回值:**
L
linhaoran 已提交
106

Z
zengyawen 已提交
107 108 109 110
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |

L
liu-ganlin 已提交
111 112 113 114 115 116 117 118
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The has method cannot be bound. |

Z
zengyawen 已提交
119 120
**示例:**

121
```ts
Z
zengyawen 已提交
122 123 124
let treeSet = new TreeSet();
treeSet.has(123);
treeSet.add(123);
125
let result1 = treeSet.has(123);
Z
zengyawen 已提交
126
```
L
linhaoran 已提交
127 128 129 130


### getFirstValue

Z
zengyawen 已提交
131
getFirstValue(): T
L
linhaoran 已提交
132 133 134

获取容器中排序第一的数据。

Z
zengyawen 已提交
135 136
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
137
**返回值:**
L
linhaoran 已提交
138

Z
zengyawen 已提交
139 140 141 142
| 类型 | 说明 |
| -------- | -------- |
| T | 返回排序第一的数据。 |

L
liu-ganlin 已提交
143 144 145 146 147 148 149 150
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getFirstValue method cannot be bound. |

Z
zengyawen 已提交
151 152
**示例:**

153
```ts
Z
zengyawen 已提交
154
let treeSet = new TreeSet();
L
lengchangjing 已提交
155 156
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
157 158
let result = treeSet.getFirstValue();
```
L
linhaoran 已提交
159 160 161 162


### getLastValue

Z
zengyawen 已提交
163
getLastValue(): T
L
linhaoran 已提交
164 165 166

获取容器中排序最后的数据。

Z
zengyawen 已提交
167 168
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
169
**返回值:**
L
linhaoran 已提交
170

Z
zengyawen 已提交
171 172 173 174
| 类型 | 说明 |
| -------- | -------- |
| T | 返回排序最后的数据。 |

L
liu-ganlin 已提交
175 176 177 178 179 180 181 182
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getLastValue method cannot be bound. |

Z
zengyawen 已提交
183 184
**示例:**

185
```ts
Z
zengyawen 已提交
186
let treeSet = new TreeSet();
L
lengchangjing 已提交
187 188
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
189 190
let result = treeSet.getLastValue();
```
L
linhaoran 已提交
191 192 193


### add
194

Z
zengyawen 已提交
195
add(value: T): boolean
L
linhaoran 已提交
196

197
向容器中添加一组数据。
L
linhaoran 已提交
198

Z
zengyawen 已提交
199 200
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
201
**参数:**
L
linhaoran 已提交
202

Z
zengyawen 已提交
203 204 205
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 添加的成员数据。 |
L
linhaoran 已提交
206

Z
zengyawen 已提交
207 208 209 210 211 212
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功添加新数据至容器返回true,否则返回false。 |

L
liu-ganlin 已提交
213 214 215 216 217 218 219 220
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The add method cannot be bound. |

Z
zengyawen 已提交
221 222
**示例:**

223
```ts
Z
zengyawen 已提交
224
let treeSet = new TreeSet();
L
lengchangjing 已提交
225
let result = treeSet.add("squirrel");
Z
zengyawen 已提交
226
```
L
linhaoran 已提交
227 228 229 230


### remove

231
remove(value: T): boolean
L
linhaoran 已提交
232 233 234

删除指定的元素。

Z
zengyawen 已提交
235 236
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
237 238 239 240
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
241
| value | T | 是 | 指定的元素。 |
L
linhaoran 已提交
242

Z
zengyawen 已提交
243
**返回值:**
L
linhaoran 已提交
244

Z
zengyawen 已提交
245 246 247 248
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功删除元素返回true,否则返回false。 |

L
liu-ganlin 已提交
249 250 251 252 253 254 255 256
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

Z
zengyawen 已提交
257 258
**示例:**

259
```ts
Z
zengyawen 已提交
260
let treeSet = new TreeSet();
L
lengchangjing 已提交
261 262 263
treeSet.add("squirrel");
treeSet.add("sparrow");
let result = treeSet.remove("sparrow");
Z
zengyawen 已提交
264
```
L
linhaoran 已提交
265 266 267 268


### getLowerValue

Z
zengyawen 已提交
269
getLowerValue(key: T): T
L
linhaoran 已提交
270 271 272

获取容器中比传入元素排序靠前一位的元素。

Z
zengyawen 已提交
273 274
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
275 276 277 278 279
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | T | 是 | 对比的元素值。 |
L
linhaoran 已提交
280

Z
zengyawen 已提交
281
**返回值:**
L
linhaoran 已提交
282

Z
zengyawen 已提交
283 284 285 286
| 类型 | 说明 |
| -------- | -------- |
| T | 返回排序中对比元素前一位的数据。 |

L
liu-ganlin 已提交
287 288 289 290 291 292 293 294
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getLowerValue method cannot be bound. |

Z
zengyawen 已提交
295 296
**示例:**

297
```ts
Z
zengyawen 已提交
298
let treeSet = new TreeSet();
L
lengchangjing 已提交
299 300 301 302
treeSet.add("squirrel");
treeSet.add("sparrow");
treeSet.add("gander");
let result = treeSet.getLowerValue("sparrow");
Z
zengyawen 已提交
303
```
L
linhaoran 已提交
304 305 306 307


### getHigherValue

Z
zengyawen 已提交
308
getHigherValue(key: T): T
L
linhaoran 已提交
309 310 311

获取容器中比传入元素排序靠后一位的元素。

Z
zengyawen 已提交
312 313
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
314
**参数:**
L
linhaoran 已提交
315

Z
zengyawen 已提交
316 317 318
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | T | 是 | 对比的元素。 |
L
linhaoran 已提交
319

Z
zengyawen 已提交
320 321 322 323
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
324
| T | 返回排序中传入元素后一位的数据。 |
Z
zengyawen 已提交
325

L
liu-ganlin 已提交
326 327 328 329 330 331 332 333
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getHigherValue method cannot be bound. |

Z
zengyawen 已提交
334 335
**示例:**

336
```ts
Z
zengyawen 已提交
337
let treeSet = new TreeSet();
L
lengchangjing 已提交
338 339 340 341
treeSet.add("squirrel");
treeSet.add("sparrow");
treeSet.add("gander");
let result = treeSet.getHigherValue("sparrow");
Z
zengyawen 已提交
342
```
L
linhaoran 已提交
343 344 345 346


### popFirst

Z
zengyawen 已提交
347
popFirst(): T
L
linhaoran 已提交
348 349 350

删除容器中排序最前的数据。

Z
zengyawen 已提交
351 352
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
353
**返回值:**
L
linhaoran 已提交
354

Z
zengyawen 已提交
355 356 357 358
| 类型 | 说明 |
| -------- | -------- |
| T | 返回删除的数据。 |

L
liu-ganlin 已提交
359 360 361 362 363 364 365 366
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The popFirst method cannot be bound. |

W
wusongqing 已提交
367
**示例:**
Z
zengyawen 已提交
368

369
```ts
Z
zengyawen 已提交
370
let treeSet = new TreeSet();
L
lengchangjing 已提交
371 372
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
373 374
let result = treeSet.popFirst();
```
L
linhaoran 已提交
375 376 377 378


### popLast

Z
zengyawen 已提交
379
popLast(): T
L
linhaoran 已提交
380 381 382

删除容器中排序最后的数据。

Z
zengyawen 已提交
383 384
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
385
**返回值:**
L
linhaoran 已提交
386

Z
zengyawen 已提交
387 388
| 类型 | 说明 |
| -------- | -------- |
389
| T | 返回删除的数据。 |
Z
zengyawen 已提交
390

L
liu-ganlin 已提交
391 392 393 394 395 396 397 398
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The popLast method cannot be bound. |

W
wusongqing 已提交
399
**示例:**
Z
zengyawen 已提交
400

401
```ts
Z
zengyawen 已提交
402
let treeSet = new TreeSet();
L
lengchangjing 已提交
403 404
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
405 406
let result = treeSet.popLast();
```
L
linhaoran 已提交
407 408 409 410


### clear

Z
zengyawen 已提交
411
clear(): void
L
linhaoran 已提交
412

Z
zengyawen 已提交
413
清除容器中的所有元素,并把length置为0。
L
linhaoran 已提交
414

Z
zengyawen 已提交
415 416
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
417 418 419 420 421 422 423 424
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

Z
zengyawen 已提交
425 426
**示例:**

427
```ts
Z
zengyawen 已提交
428
let treeSet = new TreeSet();
L
lengchangjing 已提交
429 430
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
431 432
treeSet.clear();
```
L
linhaoran 已提交
433 434 435 436


### values

Z
zengyawen 已提交
437
values(): IterableIterator&lt;T&gt;
L
linhaoran 已提交
438

W
wusongqing 已提交
439
返回包含此映射中包含的键值的新迭代器对象。
L
linhaoran 已提交
440

Z
zengyawen 已提交
441 442
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
443 444 445 446 447 448
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;T&gt; | 返回一个迭代器。 |

L
liu-ganlin 已提交
449 450 451 452 453 454 455 456
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The values method cannot be bound. |

Z
zengyawen 已提交
457 458
**示例:**

459
```ts
Z
zengyawen 已提交
460
let treeSet = new TreeSet();
L
lengchangjing 已提交
461 462
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
463 464 465
let iter = treeSet.values();
let temp = iter.next().value;
while(temp != undefined) {
466
  console.log("value:" + temp);
Z
zengyawen 已提交
467
  temp = iter.next().value;
L
liu-ganlin 已提交
468
}
Z
zengyawen 已提交
469
```
L
linhaoran 已提交
470 471 472 473


### forEach

474
forEach(callbackFn: (value?: T, key?: T, set?: TreeSet&lt;T&gt;) => void, thisArg?: Object): void
L
linhaoran 已提交
475 476 477

通过回调函数来遍历实例对象上的元素以及元素对应的下标。

Z
zengyawen 已提交
478 479
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
480 481 482 483
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
484
| callbackFn | function | 是 | 回调函数。 |
Z
zengyawen 已提交
485 486 487 488 489
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |

callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
490
| value | T | 否 | 当前遍历到的元素。 |
Z
zengyawen 已提交
491
| key | T | 否 | 当前遍历到的元素(和value相同)。 |
492
| set | TreeSet&lt;T&gt; | 否 | 当前调用forEach方法的实例对象。 |
Z
zengyawen 已提交
493

L
liu-ganlin 已提交
494 495 496 497 498 499 500 501
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

Z
zengyawen 已提交
502 503
**示例:**

504
```ts
Z
zengyawen 已提交
505
let treeSet = new TreeSet();
L
lengchangjing 已提交
506 507
treeSet.add("sparrow");
treeSet.add("gull");
Z
zengyawen 已提交
508
treeSet.forEach((value, key) => {
509
  console.log("value:" + value, key)
Z
zengyawen 已提交
510 511
});
```
L
linhaoran 已提交
512 513 514 515


### entries

Z
zengyawen 已提交
516
entries(): IterableIterator<[T, T]>
L
linhaoran 已提交
517

518
返回包含此映射中包含的键值对的新迭代器对象。
L
linhaoran 已提交
519

Z
zengyawen 已提交
520 521
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
522 523 524 525 526 527
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator<[T, T]> | 返回一个迭代器。 |

L
liu-ganlin 已提交
528 529 530 531 532 533 534 535
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The entries method cannot be bound. |

Z
zengyawen 已提交
536 537
**示例:**

538
```ts
Z
zengyawen 已提交
539
let treeSet = new TreeSet();
L
lengchangjing 已提交
540 541
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
542 543 544
let iter = treeSet.entries();
let temp = iter.next().value;
while(temp != undefined) {
545 546
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
547 548 549
  temp = iter.next().value;
}
```
L
linhaoran 已提交
550 551 552 553


### [Symbol.iterator]

Z
zengyawen 已提交
554
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
L
linhaoran 已提交
555 556 557

返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。

Z
zengyawen 已提交
558 559
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
560 561 562 563 564 565
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;T&gt; | 返回一个迭代器 |

L
liu-ganlin 已提交
566 567 568 569 570 571 572 573
**错误码:**

以下错误码的详细介绍请参见[containers错误码](../errorcodes/errorcode-containers.md)

| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

Z
zengyawen 已提交
574 575
**示例:**

576
```ts
Z
zengyawen 已提交
577
let treeSet = new TreeSet();
L
lengchangjing 已提交
578 579
treeSet.add("squirrel");
treeSet.add("sparrow");
Z
zengyawen 已提交
580 581 582
  
// 使用方法一:
for (let item of treeSet) { 
583
  console.log("value:" + item);
Z
zengyawen 已提交
584 585 586 587 588 589
}

// 使用方法二:
let iter = treeSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
590
  console.log("value:" + temp);
Z
zengyawen 已提交
591 592 593
  temp = iter.next().value;
}
```