• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.TreeMap (Nonlinear Container TreeMap)
2
3TreeMap stores key-value (KV) pairs. Each key must be unique and have only one value.
4
5TreeMap is implemented using a red-black tree, which is a binary search tree where keys are stored in sorted order for efficient insertion and removal.
6
7[HashMap](js-apis-hashmap.md) is faster in accessing data than TreeMap, because the former accesses data based on the hash code of the key, whereas the latter stores and accesses the keys in sorted order.
8
9Recommended use case: Use TreeMap when you need to store KV pairs in sorted order.
10
11This topic uses the following to identify the use of generics:
12
13- K: Key
14
15- V: Value
16
17> **NOTE**
18>
19> 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.
20
21
22## Modules to Import
23
24```ts
25import { TreeMap } from '@kit.ArkTS';
26```
27
28## TreeMap
29
30### Properties
31
32**Atomic service API**: This API can be used in atomic services since API version 12.
33
34**System capability**: SystemCapability.Utils.Lang
35
36| Name| Type| Readable| Writable| Description|
37| -------- | -------- | -------- | -------- | -------- |
38| length | number | Yes| No| Number of elements in a TreeMap.|
39
40
41### constructor
42
43constructor(comparator?:(firstValue: K, secondValue: K) => boolean)
44
45A constructor used to create a **TreeMap** instance. It supports sorting elements in ascending or descending order by using comparators.
46
47**Atomic service API**: This API can be used in atomic services since API version 12.
48
49**System capability**: SystemCapability.Utils.Lang
50
51**Parameters**
52
53| Name| Type| Mandatory| Description|
54| -------- | -------- | -------- | -------- |
55| comparator | function | No| Custom comparator, which can be used to sort elements based on the comparison relationship. The default value is **hole** (a blank placeholder), indicating that no comparator is provided.|
56
57comparator parameters
58
59| Name| Type| Mandatory| Description|
60| ------| ---- | ----- | -- |
61| firstValue | K | Yes| Previous element.|
62| secondValue | K | Yes| Next element.|
63
64**Error codes**
65
66For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
67
68| ID| Error Message|
69| -------- | -------- |
70| 401      | Parameter error. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. |
71| 10200012 | The TreeMap's constructor cannot be directly invoked. |
72
73**Example**
74
75```ts
76// Default constructor.
77let treeMap : TreeMap<number, number> = new TreeMap();
78```
79
80```ts
81// Use the comparator firstValue < secondValue if the elements are expected to be sorted in ascending order. Use firstValue > secondValue if the elements are expected to be sorted in descending order.
82let treeMap : TreeMap<string,string> = new TreeMap<string,string>((firstValue: string, secondValue: string) : boolean => {return firstValue > secondValue});
83treeMap.set("aa","3");
84treeMap.set("dd","1");
85treeMap.set("cc","2");
86treeMap.set("bb","4");
87let numbers = Array.from(treeMap.keys());
88for (let item of numbers) {
89  console.info("treeMap: " + item);
90}
91// Output:
92// treeMap: dd
93// treeMap: cc
94// treeMap: bb
95// treeMap: aa
96```
97
98```ts
99// When a custom type is inserted, a comparator must be provided.
100 class TestEntry{
101   public id: number = 0;
102 }
103 let ts1: TreeMap<TestEntry, string> = new TreeMap<TestEntry, string>((t1: TestEntry, t2: TestEntry): boolean => {return t1.id < t2.id;});
104 let entry1: TestEntry = {
105   id: 0
106 };
107 let entry2: TestEntry = {
108   id: 1
109 }
110 ts1.set(entry1, "0");
111 ts1.set(entry2, "1");
112 console.info("length: " + ts1.length); // length: 2
113
114```
115
116
117### isEmpty
118
119isEmpty(): boolean
120
121Checks whether this TreeMap is empty (contains no element).
122
123**Atomic service API**: This API can be used in atomic services since API version 12.
124
125**System capability**: SystemCapability.Utils.Lang
126
127**Return value**
128
129| Type| Description|
130| -------- | -------- |
131| boolean | Returns **true** if the TreeMap is empty; returns **false** otherwise.|
132
133**Error codes**
134
135For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
136
137| ID| Error Message|
138| -------- | -------- |
139| 10200011 | The isEmpty method cannot be bound. |
140
141**Example**
142
143```ts
144let treeMap : TreeMap<number, number> = new TreeMap();
145let result = treeMap.isEmpty(); // result: true
146```
147
148
149### hasKey
150
151hasKey(key: K): boolean
152
153Checks whether this TreeMap has the specified key.
154
155**Atomic service API**: This API can be used in atomic services since API version 12.
156
157**System capability**: SystemCapability.Utils.Lang
158
159**Parameters**
160
161| Name| Type| Mandatory| Description|
162| -------- | -------- | -------- | -------- |
163| key | K | Yes| Target key.|
164
165**Return value**
166
167| Type| Description|
168| -------- | -------- |
169| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
170
171**Error codes**
172
173For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
174
175| ID| Error Message|
176| -------- | -------- |
177| 10200011 | The hasKey method cannot be bound. |
178
179**Example**
180
181```ts
182let treeMap : TreeMap<string, number> = new TreeMap();
183treeMap.set("squirrel", 123);
184let result = treeMap.hasKey("squirrel"); // result: true
185```
186
187
188### hasValue
189
190hasValue(value: V): boolean
191
192Checks whether this TreeMap has the specified key.
193
194**Atomic service API**: This API can be used in atomic services since API version 12.
195
196**System capability**: SystemCapability.Utils.Lang
197
198**Parameters**
199
200| Name| Type| Mandatory| Description|
201| -------- | -------- | -------- | -------- |
202| value | V | Yes| Target value.|
203
204**Return value**
205
206| Type| Description|
207| -------- | -------- |
208| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
209
210**Error codes**
211
212For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
213
214| ID| Error Message|
215| -------- | -------- |
216| 10200011 | The hasValue method cannot be bound. |
217
218**Example**
219
220```ts
221let treeMap : TreeMap<string, number> = new TreeMap();
222treeMap.set("squirrel", 123);
223let result = treeMap.hasValue(123); // result: true
224```
225
226
227### get
228
229get(key: K): V
230
231Obtains the value of the specified key in this TreeMap.
232
233**Atomic service API**: This API can be used in atomic services since API version 12.
234
235**System capability**: SystemCapability.Utils.Lang
236
237**Parameters**
238
239| Name| Type| Mandatory| Description|
240| -------- | -------- | -------- | -------- |
241| key | K | Yes| Target key.|
242
243**Return value**
244
245| Type| Description|
246| -------- | -------- |
247| V | Value obtained. If nothing is obtained, **undefined** is returned.|
248
249**Error codes**
250
251For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
252
253| ID| Error Message|
254| -------- | -------- |
255| 10200011 | The get method cannot be bound. |
256
257**Example**
258
259```ts
260let treeMap : TreeMap<string, number> = new TreeMap();
261treeMap.set("squirrel", 123);
262treeMap.set("sparrow", 356);
263let result = treeMap.get("sparrow");
264console.info("result:", result); // result: 356
265```
266
267
268### getFirstKey
269
270getFirstKey(): K
271
272Obtains the first key in this TreeMap.
273
274**Atomic service API**: This API can be used in atomic services since API version 12.
275
276**System capability**: SystemCapability.Utils.Lang
277
278**Return value**
279
280| Type| Description|
281| -------- | -------- |
282| K | Key obtained. If nothing is obtained, **undefined** is returned.|
283
284**Error codes**
285
286For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
287
288| ID| Error Message|
289| -------- | -------- |
290| 10200011 | The getFirstKey method cannot be bound. |
291
292**Example**
293
294```ts
295let treeMap : TreeMap<string, number> = new TreeMap();
296treeMap.set("squirrel", 123);
297treeMap.set("sparrow", 356);
298let result = treeMap.getFirstKey();
299console.info("result:", result); // result: sparrow
300```
301
302
303### getLastKey
304
305getLastKey(): K
306
307Obtains the last key in this TreeMap.
308
309**Atomic service API**: This API can be used in atomic services since API version 12.
310
311**System capability**: SystemCapability.Utils.Lang
312
313**Return value**
314
315| Type| Description|
316| -------- | -------- |
317| K | Key obtained. If nothing is obtained, **undefined** is returned.|
318
319**Error codes**
320
321For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
322
323| ID| Error Message|
324| -------- | -------- |
325| 10200011 | The getLastKey method cannot be bound. |
326
327**Example**
328
329```ts
330let treeMap : TreeMap<string, number> = new TreeMap();
331treeMap.set("squirrel", 123);
332treeMap.set("sparrow", 356);
333let result = treeMap.getLastKey();
334console.info("result:", result); // result: squirrel
335```
336
337
338### setAll
339
340setAll(map: TreeMap<K, V>): void
341
342Adds all elements in a **TreeMap** instance to this TreeMap.
343
344**Atomic service API**: This API can be used in atomic services since API version 12.
345
346**System capability**: SystemCapability.Utils.Lang
347
348**Parameters**
349
350| Name| Type| Mandatory| Description|
351| -------- | -------- | -------- | -------- |
352| map | TreeMap<K, V> | Yes| **TreeMap** object to be added to the TreeMap.|
353
354**Error codes**
355
356For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
357
358| ID| Error Message|
359| -------- | -------- |
360| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
361| 10200011 | The setAll method cannot be bound. |
362
363**Example**
364
365```ts
366let treeMap : TreeMap<string, number> = new TreeMap();
367treeMap.set("squirrel", 123);
368treeMap.set("sparrow", 356);
369let map : TreeMap<string, number> = new TreeMap();
370map.set("demo", 12);
371map.setAll(treeMap); // Add all elements in the treeMap to the map.
372map.forEach((value ?: number, key ?: string) : void => {
373  console.log("value: " + value, "key: " + key);
374})
375// Output:
376// value: 12 key: demo
377// value: 356 key: sparrow
378// value: 123 key: squirrel
379```
380
381
382### set
383
384set(key: K, value: V): Object
385
386Adds or updates an element in this TreeMap.
387
388**Atomic service API**: This API can be used in atomic services since API version 12.
389
390**System capability**: SystemCapability.Utils.Lang
391
392**Parameters**
393
394| Name| Type| Mandatory| Description|
395| -------- | -------- | -------- | -------- |
396| key | K | Yes| Key of the target element.|
397| value | V | Yes| Value of the target element.|
398
399**Return value**
400
401| Type| Description|
402| -------- | -------- |
403| Object | TreeMap that contains the new element.|
404
405**Error codes**
406
407For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
408
409| ID| Error Message|
410| -------- | -------- |
411| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
412| 10200011 | The set method cannot be bound. |
413
414**Example**
415
416```ts
417let treeMap : TreeMap<string, number> = new TreeMap();
418treeMap.set("squirrel", 123); // Add data.
419```
420
421
422### remove
423
424remove(key: K): V
425
426Removes the element with the specified key from this TreeMap.
427
428**Atomic service API**: This API can be used in atomic services since API version 12.
429
430**System capability**: SystemCapability.Utils.Lang
431
432**Parameters**
433
434| Name| Type| Mandatory| Description|
435| -------- | -------- | -------- | -------- |
436| key | K | Yes| Target key.|
437
438**Return value**
439
440| Type| Description|
441| -------- | -------- |
442| V | Value of the element removed.|
443
444**Error codes**
445
446For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
447
448| ID| Error Message|
449| -------- | -------- |
450| 10200011 | The remove method cannot be bound. |
451
452**Example**
453
454```ts
455let treeMap : TreeMap<string, number> = new TreeMap();
456treeMap.set("squirrel", 123);
457treeMap.set("sparrow", 356);
458let result = treeMap.remove("sparrow"); // Delete data.
459```
460
461
462### getLowerKey
463
464getLowerKey(key: K): K
465
466Obtains the largest key in this TreeMap that is less than the specified comparison key.
467
468**Atomic service API**: This API can be used in atomic services since API version 12.
469
470**System capability**: SystemCapability.Utils.Lang
471
472**Parameters**
473
474| Name| Type| Mandatory| Description|
475| -------- | -------- | -------- | -------- |
476| key | K | Yes| Input key.|
477
478**Return value**
479
480| Type| Description|
481| -------- | -------- |
482| K | Key obtained. If nothing is obtained, **undefined** is returned.|
483
484**Error codes**
485
486For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
487
488| ID| Error Message|
489| -------- | -------- |
490| 10200011 | The getLowerKey method cannot be bound. |
491
492**Example**
493
494```ts
495let treeMap : TreeMap<number, string> = new TreeMap();
496treeMap.set(1, 'one');
497treeMap.set(2, 'two');
498treeMap.set(3, 'three');
499treeMap.set(4, 'four');
500let result = treeMap.getLowerKey(3);
501console.info("result:", result); // result: 2
502```
503
504
505### getHigherKey
506
507getHigherKey(key: K): K
508
509Obtains the smallest key in this TreeMap that is greater than the specified comparison key.
510
511**Atomic service API**: This API can be used in atomic services since API version 12.
512
513**System capability**: SystemCapability.Utils.Lang
514
515**Parameters**
516
517| Name| Type| Mandatory| Description|
518| -------- | -------- | -------- | -------- |
519| key | K | Yes| Input key.|
520
521**Return value**
522
523| Type| Description|
524| -------- | -------- |
525| K | Key obtained. If nothing is obtained, **undefined** is returned.|
526
527**Error codes**
528
529For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
530
531| ID| Error Message|
532| -------- | -------- |
533| 10200011 | The getHigherKey method cannot be bound. |
534
535**Example**
536
537```ts
538let treeMap : TreeMap<number, string> = new TreeMap();
539treeMap.set(1, 'one');
540treeMap.set(2, 'two');
541treeMap.set(3, 'three');
542treeMap.set(4, 'four');
543let result = treeMap.getHigherKey(3);
544console.info("result:", result); // result: 4
545```
546
547### replace
548
549replace(key: K, newValue: V): boolean
550
551Replaces an element in this TreeMap.
552
553**Atomic service API**: This API can be used in atomic services since API version 12.
554
555**System capability**: SystemCapability.Utils.Lang
556
557**Parameters**
558
559| Name| Type| Mandatory| Description|
560| -------- | -------- | -------- | -------- |
561| key | K | Yes| Key of the target element.|
562| newValue | V | Yes| New value of the element.|
563
564**Return value**
565
566| Type| Description|
567| -------- | -------- |
568| boolean | Returns **true** if the element is replaced successfully; returns **false** otherwise.|
569
570**Error codes**
571
572For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
573
574| ID| Error Message|
575| -------- | -------- |
576| 10200011 | The replace method cannot be bound. |
577
578**Example**
579
580```ts
581let treeMap : TreeMap<string, number> = new TreeMap();
582treeMap.set("sparrow", 123);
583let result = treeMap.replace("sparrow", 357);
584console.info("sparrow:", treeMap.get("sparrow")); // sparrow: 357
585```
586
587
588### clear
589
590clear(): void
591
592Clears this TreeMap and sets its length to **0**.
593
594**Atomic service API**: This API can be used in atomic services since API version 12.
595
596**System capability**: SystemCapability.Utils.Lang
597
598**Error codes**
599
600For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
601
602| ID| Error Message|
603| -------- | -------- |
604| 10200011 | The clear method cannot be bound. |
605
606**Example**
607
608```ts
609let treeMap : TreeMap<string, number> = new TreeMap();
610treeMap.set("squirrel", 123);
611treeMap.set("sparrow", 356);
612treeMap.clear();
613let result = treeMap.isEmpty();
614console.info("result:", result); // result: true
615```
616
617
618### keys
619
620keys(): IterableIterator&lt;K&gt;
621
622Returns an iterator that contains all the keys in this TreeMap.
623
624**Atomic service API**: This API can be used in atomic services since API version 12.
625
626**System capability**: SystemCapability.Utils.Lang
627
628**Return value**
629
630| Type| Description|
631| -------- | -------- |
632| IterableIterator&lt;K&gt; | Iterator obtained.|
633
634**Error codes**
635
636For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
637
638| ID| Error Message|
639| -------- | -------- |
640| 10200011 | The keys method cannot be bound. |
641
642**Example**
643
644```ts
645let treeMap : TreeMap<string, number> = new TreeMap();
646treeMap.set("squirrel", 123);
647treeMap.set("sparrow", 356);
648let it = treeMap.keys();
649let t: IteratorResult<string> = it.next();
650while(!t.done) {
651  console.info("TreeMap:", t.value);
652  t = it.next();
653}
654// Output:
655// TreeMap: sparrow
656// TreeMap: squirrel
657```
658
659
660### values
661
662values(): IterableIterator&lt;V&gt;
663
664Returns an iterator that contains all the values in this TreeMap.
665
666**Atomic service API**: This API can be used in atomic services since API version 12.
667
668**System capability**: SystemCapability.Utils.Lang
669
670**Return value**
671
672| Type| Description|
673| -------- | -------- |
674| IterableIterator&lt;V&gt; | Iterator obtained.|
675
676**Error codes**
677
678For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
679
680| ID| Error Message|
681| -------- | -------- |
682| 10200011 | The values method cannot be bound. |
683
684**Example**
685
686```ts
687let treeMap : TreeMap<string, number> = new TreeMap();
688treeMap.set("squirrel", 123);
689treeMap.set("sparrow", 356);
690let it = treeMap.values();
691let t: IteratorResult<number> = it.next();
692while(!t.done) {
693  console.info("TreeMap:", t.value);
694  t = it.next();
695}
696// Output:
697// TreeMap: 356
698// TreeMap: 123
699```
700
701
702### forEach
703
704forEach(callbackFn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void
705
706Uses a callback to traverse the elements in this TreeMap and obtain their indexes.
707
708**Atomic service API**: This API can be used in atomic services since API version 12.
709
710**System capability**: SystemCapability.Utils.Lang
711
712**Parameters**
713
714| Name| Type| Mandatory| Description|
715| -------- | -------- | -------- | -------- |
716| callbackFn | function | Yes| Callback invoked to traverse the elements in the TreeMap.|
717| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
718
719callbackFn
720| Name| Type| Mandatory| Description|
721| -------- | -------- | -------- | -------- |
722| value | V | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.|
723| key | K | No| Key of the element that is currently traversed. The default value is the key of the first key-value pair.|
724| map | TreeMap<K, V> | No| Instance that calls the **forEach** API. The default value is this instance.|
725
726**Error codes**
727
728For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
729
730| ID| Error Message|
731| -------- | -------- |
732| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
733| 10200011 | The forEach method cannot be bound. |
734
735**Example**
736
737```ts
738let treeMap : TreeMap<string, number> = new TreeMap();
739treeMap.set("sparrow", 123);
740treeMap.set("gull", 357);
741treeMap.forEach((value ?: number, key ?: string) : void => {
742  console.info("value: " + value, "key: " + key);
743});
744// Output:
745// value: 357 key: gull
746// value: 123 key: sparrow
747```
748```ts
749 // You are not advised to use the set or remove APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
750 let treeMap : TreeMap<string, number> = new TreeMap();
751 for(let i = 0; i < 10; i++) {
752   treeMap.set("sparrow" + i, 123);
753 }
754 for(let i = 0;i < 10; i++) {
755   treeMap.remove("sparrow" + i);
756 }
757```
758
759### entries
760
761entries(): IterableIterator<[K, V]>
762
763Returns an iterator that contains all the elements in this TreeMap.
764
765**Atomic service API**: This API can be used in atomic services since API version 12.
766
767**System capability**: SystemCapability.Utils.Lang
768
769**Return value**
770
771| Type| Description|
772| -------- | -------- |
773| IterableIterator<[K, V]> | Iterator obtained.|
774
775**Error codes**
776
777For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
778
779| ID| Error Message|
780| -------- | -------- |
781| 10200011 | The entries method cannot be bound. |
782
783**Example**
784
785```ts
786let treeMap : TreeMap<string, number> = new TreeMap();
787treeMap.set("squirrel", 123);
788treeMap.set("sparrow", 356);
789let it = treeMap.entries();
790let t: IteratorResult<Object[]> = it.next();
791while(!t.done) {
792  console.info("TreeMap:", t.value);
793  t = it.next()
794}
795// Output:
796// TreeMap: sparrow,356
797// TreeMap: squirrel,123
798```
799
800```ts
801 // You are not advised to use the set or remove APIs in entries because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
802 let treeMap : TreeMap<string, number> = new TreeMap();
803 for(let i = 0; i < 10; i++) {
804   treeMap.set("sparrow" + i, 123);
805 }
806 for(let i = 0;i < 10; i++) {
807   treeMap.remove("sparrow" + i);
808 }
809```
810
811### [Symbol.iterator]
812
813[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
814
815Returns an iterator, each item of which is a JavaScript object.
816
817**Atomic service API**: This API can be used in atomic services since API version 12.
818
819**System capability**: SystemCapability.Utils.Lang
820
821**Return value**
822| Type| Description|
823| -------- | -------- |
824| IterableIterator<[K, V]> | Iterator obtained.|
825
826**Error codes**
827
828For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
829
830| ID| Error Message|
831| -------- | -------- |
832| 10200011 | The Symbol.iterator method cannot be bound. |
833
834**Example**
835
836```ts
837let treeMap : TreeMap<string, number> = new TreeMap();
838treeMap.set("squirrel", 123);
839treeMap.set("sparrow", 356);
840
841// Method 1:
842let it = treeMap.entries();
843let t: IteratorResult<Object[]> = it.next();
844while(!t.done) {
845  console.info("TreeMap:", t.value);
846  t = it.next();
847}
848// Output:
849// TreeMap: sparrow,356
850// TreeMap: squirrel,123
851
852// Method 2:
853let iter = treeMap[Symbol.iterator]();
854let temp: IteratorResult<Object[]> = iter.next();
855while(!temp.done) {
856  console.info("key:", temp.value[0]);
857  console.info("value:", temp.value[1]);
858  temp = iter.next();
859}
860// Output:
861// key: sparrow
862// value: 356
863// key: squirrel
864// value: 123
865```
866
867```ts
868 // You are not advised to use the set or remove APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
869 let treeMap : TreeMap<string, number> = new TreeMap();
870 for(let i = 0; i < 10; i++) {
871   treeMap.set("sparrow" + i, 123);
872 }
873 for(let i = 0;i < 10; i++) {
874   treeMap.remove("sparrow" + i);
875 }
876```
877