• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 allow only unique elements. However, null values are allowed in **HashSet**, but not in **TreeSet**, because null values may affect the order of elements in the container.
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. The default value is **hole** (a blank placeholder), indicating that no comparator. is provided.|
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 : TreeSet<string | number | boolean | Object> = 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 : TreeSet<string | number | boolean | Object>  = 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 : TreeSet<number> = new TreeSet();
126treeSet.add(123);
127let result = treeSet.has(123);
128```
129
130### getFirstValue
131
132getFirstValue(): T
133
134Obtains the value of the first element in this container.
135
136**System capability**: SystemCapability.Utils.Lang
137
138**Return value**
139
140| Type| Description|
141| -------- | -------- |
142| T | Value obtained.|
143
144**Error codes**
145
146For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
147
148| ID| Error Message|
149| -------- | -------- |
150| 10200011 | The getFirstValue method cannot be bound. |
151
152**Example**
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
166Obtains the value of the last element in this container.
167
168**System capability**: SystemCapability.Utils.Lang
169
170**Return value**
171
172| Type| Description|
173| -------- | -------- |
174| T | Value obtained.|
175
176**Error codes**
177
178For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
179
180| ID| Error Message|
181| -------- | -------- |
182| 10200011 | The getLastValue method cannot be bound. |
183
184**Example**
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
198Adds an element to this container.
199
200**System capability**: SystemCapability.Utils.Lang
201
202**Parameters**
203
204| Name| Type| Mandatory| Description|
205| -------- | -------- | -------- | -------- |
206| value | T | Yes| Target element.|
207
208**Return value**
209
210| Type| Description|
211| -------- | -------- |
212| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
213
214**Error codes**
215
216For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
217
218| ID| Error Message|
219| -------- | -------- |
220| 10200011 | The add method cannot be bound. |
221
222**Example**
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
234Removes the element with the specified key from this container.
235
236**System capability**: SystemCapability.Utils.Lang
237
238**Parameters**
239
240| Name| Type| Mandatory| Description|
241| -------- | -------- | -------- | -------- |
242| value | T | Yes| Key of the target element.|
243
244**Return value**
245
246| Type| Description|
247| -------- | -------- |
248| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
249
250**Error codes**
251
252For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
253
254| ID| Error Message|
255| -------- | -------- |
256| 10200011 | The remove method cannot be bound. |
257
258**Example**
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
272Obtains the value that is placed in front of the input key in this container.
273
274**System capability**: SystemCapability.Utils.Lang
275
276**Parameters**
277
278| Name| Type| Mandatory| Description|
279| -------- | -------- | -------- | -------- |
280| key | T | Yes| Input key.|
281
282**Return value**
283
284| Type| Description|
285| -------- | -------- |
286| T | Value obtained.|
287
288**Error codes**
289
290For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
291
292| ID| Error Message|
293| -------- | -------- |
294| 10200011 | The getLowerValue method cannot be bound. |
295
296**Example**
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
311Obtains the value that is placed next to the input key in this container.
312
313**System capability**: SystemCapability.Utils.Lang
314
315**Parameters**
316
317| Name| Type| Mandatory| Description|
318| -------- | -------- | -------- | -------- |
319| key | T | Yes| Input key.|
320
321**Return value**
322
323| Type| Description|
324| -------- | -------- |
325| T | Value obtained.|
326
327**Error codes**
328
329For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
330
331| ID| Error Message|
332| -------- | -------- |
333| 10200011 | The getHigherValue method cannot be bound. |
334
335**Example**
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
350Removes the first element in this container.
351
352**System capability**: SystemCapability.Utils.Lang
353
354**Return value**
355
356| Type| Description|
357| -------- | -------- |
358| T | Element removed.|
359
360**Error codes**
361
362For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
363
364| ID| Error Message|
365| -------- | -------- |
366| 10200011 | The popFirst method cannot be bound. |
367
368**Example**
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
382Removes the last element in this container.
383
384**System capability**: SystemCapability.Utils.Lang
385
386**Return value**
387
388| Type| Description|
389| -------- | -------- |
390| T | Element removed.|
391
392**Error codes**
393
394For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
395
396| ID| Error Message|
397| -------- | -------- |
398| 10200011 | The popLast method cannot be bound. |
399
400**Example**
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
414Clears this container and sets its length to **0**.
415
416**System capability**: SystemCapability.Utils.Lang
417
418**Error codes**
419
420For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
421
422| ID| Error Message|
423| -------- | -------- |
424| 10200011 | The clear method cannot be bound. |
425
426**Example**
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
440Obtains an iterator that contains all the values in this container.
441
442**System capability**: SystemCapability.Utils.Lang
443
444**Return value**
445
446| Type| Description|
447| -------- | -------- |
448| IterableIterator&lt;T&gt; | Iterator obtained.|
449
450**Error codes**
451
452For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
453
454| ID| Error Message|
455| -------- | -------- |
456| 10200011 | The values method cannot be bound. |
457
458**Example**
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
477Uses a callback to traverse the elements in this container and obtain their position indexes.
478
479**System capability**: SystemCapability.Utils.Lang
480
481**Parameters**
482
483| Name| Type| Mandatory| Description|
484| -------- | -------- | -------- | -------- |
485| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
486| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
487
488callbackFn
489| Name| Type| Mandatory| Description|
490| -------- | -------- | -------- | -------- |
491| value | T | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.|
492| key | T | No| Key of the element that is currently traversed. The default value is the key of the first key-value pair.|
493| set | TreeSet&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
494
495**Error codes**
496
497For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
498
499| ID| Error Message|
500| -------- | -------- |
501| 10200011 | The forEach method cannot be bound. |
502
503**Example**
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
519Obtains an iterator that contains all the elements in this container.
520
521**System capability**: SystemCapability.Utils.Lang
522
523**Return value**
524
525| Type| Description|
526| -------- | -------- |
527| IterableIterator<[T, T]> | Iterator obtained.|
528
529**Error codes**
530
531For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
532
533| ID| Error Message|
534| -------- | -------- |
535| 10200011 | The entries method cannot be bound. |
536
537**Example**
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
556Obtains an iterator, each item of which is a JavaScript object.
557
558> **NOTE**
559>
560> This API cannot be used in .ets files.
561
562**System capability**: SystemCapability.Utils.Lang
563
564**Return value**
565
566| Type| Description|
567| -------- | -------- |
568| IterableIterator&lt;T&gt; | Iterator obtained.|
569
570**Error codes**
571
572For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
573
574| ID| Error Message|
575| -------- | -------- |
576| 10200011 | The Symbol.iterator method cannot be bound. |
577
578**Example**
579
580```ts
581let treeSet : TreeSet<string> = new TreeSet();
582treeSet.add("squirrel");
583treeSet.add("sparrow");
584let numbers = Array.from(treeSet.values())
585// Method 1:
586for (let item of numbers) {
587  console.log("value:" + item);
588}
589// Method 2:
590let iter = treeSet[Symbol.iterator]();
591let temp: IteratorResult<string> = iter.next().value;
592while(temp != undefined) {
593  console.log("value:" + temp);
594  temp = iter.next().value;
595}
596```
597