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