js-apis-treemap.md 15.3 KB
Newer Older
1
# @ohos.util.TreeMap (Nonlinear Container TreeMap)
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4 5 6 7 8 9
**TreeMap** stores key-value (KV) pairs. Each key must be unique and have only one value.

**TreeMap** 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.

**[HashMap](js-apis-treemap.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.

Recommended use case: Use **TreeMap** when you need to store KV pairs in sorted order.
W
wusongqing 已提交
10

G
Gloria 已提交
11
This topic uses the following to identify the use of generics:
G
Gloria 已提交
12

G
Gloria 已提交
13
- K: Key
G
Gloria 已提交
14

G
Gloria 已提交
15 16
- V: Value

G
Gloria 已提交
17 18 19 20
> **NOTE**
>
> 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.

W
wusongqing 已提交
21 22
## Modules to Import

W
wusongqing 已提交
23
```ts
W
wusongqing 已提交
24
import TreeMap from '@ohos.util.TreeMap';  
W
wusongqing 已提交
25 26 27 28 29 30
```

## TreeMap

### Attributes

W
wusongqing 已提交
31 32
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
33 34
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
35
| length | number | Yes| No| Number of elements in a tree map (called container later).|
W
wusongqing 已提交
36 37 38 39 40 41 42 43


### constructor

constructor(comparator?:(firstValue: K, secondValue: K) => boolean)

A constructor used to create a **TreeMap** instance.

W
wusongqing 已提交
44 45
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
46 47 48 49 50 51
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Custom comparator.|

G
Gloria 已提交
52 53
**Error codes**

54
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
55 56 57 58 59

| ID| Error Message|
| -------- | -------- |
| 10200012 | The TreeMap's constructor cannot be directly invoked. |

W
wusongqing 已提交
60 61
**Example**

W
wusongqing 已提交
62
```ts
W
wusongqing 已提交
63 64 65 66 67 68
let treeMap = new TreeMap();
```


### isEmpty

W
wusongqing 已提交
69
isEmpty(): boolean
W
wusongqing 已提交
70

W
wusongqing 已提交
71 72 73
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
74 75 76 77 78 79 80

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

G
Gloria 已提交
81 82
**Error codes**

83
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
84 85 86 87 88

| ID| Error Message|
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

W
wusongqing 已提交
89 90
**Example**

W
wusongqing 已提交
91
```ts
W
wusongqing 已提交
92 93 94 95 96 97 98
const treeMap = new TreeMap();
let result = treeMap.isEmpty();
```


### hasKey

W
wusongqing 已提交
99
hasKey(key: K): boolean
W
wusongqing 已提交
100 101 102

Checks whether this container has the specified key.

W
wusongqing 已提交
103 104
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
105 106 107 108
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
109
| key | K | Yes| Target key.|
W
wusongqing 已提交
110 111 112 113 114 115 116

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|

G
Gloria 已提交
117 118
**Error codes**

119
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
120 121 122 123 124

| ID| Error Message|
| -------- | -------- |
| 10200011 | The hasKey method cannot be bound. |

W
wusongqing 已提交
125 126
**Example**

W
wusongqing 已提交
127
```ts
W
wusongqing 已提交
128
let treeMap = new TreeMap();
G
Gloria 已提交
129 130 131
let result = treeMap.hasKey("squirrel");
treeMap.set("squirrel", 123);
let result1 = treeMap.hasKey("squirrel");
W
wusongqing 已提交
132 133 134 135 136 137 138 139 140
```


### hasValue

hasValue(value: V): boolean

Checks whether this container has the specified value.

W
wusongqing 已提交
141 142
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
143 144 145 146
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
147
| value | V | Yes| Target value.|
W
wusongqing 已提交
148 149 150 151 152 153 154

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|

G
Gloria 已提交
155 156
**Error codes**

157
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
158 159 160 161 162

| ID| Error Message|
| -------- | -------- |
| 10200011 | The hasValue method cannot be bound. |

W
wusongqing 已提交
163 164
**Example**

W
wusongqing 已提交
165
```ts
W
wusongqing 已提交
166 167
let treeMap = new TreeMap();
let result = treeMap.hasValue(123);
G
Gloria 已提交
168
treeMap.set("squirrel", 123);
W
wusongqing 已提交
169 170 171 172 173 174 175 176 177 178
let result1 = treeMap.hasValue(123);
```


### get

get(key: K): V

Obtains the value of the specified key in this container.

W
wusongqing 已提交
179 180
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
181 182 183 184
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
185
| key | K | Yes| Target key.|
W
wusongqing 已提交
186 187 188 189 190 191 192

**Return value**

| Type| Description|
| -------- | -------- |
| V | Value of the key.|

G
Gloria 已提交
193 194
**Error codes**

195
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
196 197 198 199 200

| ID| Error Message|
| -------- | -------- |
| 10200011 | The get method cannot be bound. |

W
wusongqing 已提交
201 202
**Example**

W
wusongqing 已提交
203
```ts
W
wusongqing 已提交
204
let treeMap = new TreeMap();
G
Gloria 已提交
205 206 207
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
let result = treeMap.get("sparrow");
W
wusongqing 已提交
208 209 210 211 212
```


### getFirstKey

W
wusongqing 已提交
213
getFirstKey(): K
W
wusongqing 已提交
214 215 216

Obtains the first key in this container.

W
wusongqing 已提交
217 218
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
219 220 221 222 223 224
**Return value**

| Type| Description|
| -------- | -------- |
| K | Key obtained.|

G
Gloria 已提交
225 226
**Error codes**

227
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
228 229 230 231 232

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getFirstKey method cannot be bound. |

W
wusongqing 已提交
233 234
**Example**

W
wusongqing 已提交
235
```ts
W
wusongqing 已提交
236
let treeMap = new TreeMap();
G
Gloria 已提交
237 238
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
239 240 241 242 243 244
let result = treeMap.getFirstKey();
```


### getLastKey

W
wusongqing 已提交
245
getLastKey(): K
W
wusongqing 已提交
246 247 248

Obtains the last key in this container.

W
wusongqing 已提交
249 250
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
251 252 253 254 255 256
**Return value**

| Type| Description|
| -------- | -------- |
| K | Key obtained.|

G
Gloria 已提交
257 258
**Error codes**

259
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
260 261 262 263 264

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getLastKey method cannot be bound. |

W
wusongqing 已提交
265 266
**Example**

W
wusongqing 已提交
267
```ts
W
wusongqing 已提交
268
let treeMap = new TreeMap();
G
Gloria 已提交
269 270
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
271 272 273 274 275 276 277 278
let result = treeMap.getLastKey();
```


### setAll

setAll(map: TreeMap<K, V>): void

W
wusongqing 已提交
279 280 281
Adds all elements in a **TreeMap** instance to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
282 283 284 285 286

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
287
| map | TreeMap<K, V> | Yes| **TreeMap** object to be added to the container.|
W
wusongqing 已提交
288

G
Gloria 已提交
289 290
**Error codes**

291
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
292 293 294 295 296

| ID| Error Message|
| -------- | -------- |
| 10200011 | The setAll method cannot be bound. |

W
wusongqing 已提交
297 298
**Example**

W
wusongqing 已提交
299
```ts
W
wusongqing 已提交
300
let treeMap = new TreeMap();
G
Gloria 已提交
301 302
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
303
let map = new TreeMap();
304 305 306 307 308
map.set("demo", 12);
map.setAll(treeMap); // Add all elements in the treeMap to the map.
map.forEach((value, key) => {
    console.log("test" + value, key); // Print result: 12 demo, 356 sparrow, and 123 squirrel
})
W
wusongqing 已提交
309 310 311 312
```


### set
W
wusongqing 已提交
313

W
wusongqing 已提交
314 315
set(key: K, value: V): Object

W
wusongqing 已提交
316 317 318
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
319 320 321 322 323

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
324 325
| key | K | Yes| Key of the target element.|
| value | V | Yes| Value of the target element.|
W
wusongqing 已提交
326 327 328 329 330

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
331
| Object | Container that contains the new element.|
W
wusongqing 已提交
332

G
Gloria 已提交
333 334
**Error codes**

335
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
336 337 338 339 340

| ID| Error Message|
| -------- | -------- |
| 10200011 | The set method cannot be bound. |

W
wusongqing 已提交
341 342
**Example**

W
wusongqing 已提交
343
```ts
W
wusongqing 已提交
344
let treeMap = new TreeMap();
G
Gloria 已提交
345
treeMap.set("squirrel", 123);
W
wusongqing 已提交
346 347 348 349 350
```


### remove

W
wusongqing 已提交
351
remove(key: K): V
W
wusongqing 已提交
352

W
wusongqing 已提交
353 354 355
Removes the element with the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
356 357 358 359 360

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
361
| key | K | Yes| Target key.|
W
wusongqing 已提交
362 363 364 365 366

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
367
| V | Value of the element removed.|
W
wusongqing 已提交
368

G
Gloria 已提交
369 370
**Error codes**

371
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
372 373 374 375 376

| ID| Error Message|
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

W
wusongqing 已提交
377 378
**Example**

W
wusongqing 已提交
379
```ts
W
wusongqing 已提交
380
let treeMap = new TreeMap();
G
Gloria 已提交
381 382 383
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
treeMap.remove("sparrow");
W
wusongqing 已提交
384 385 386 387 388 389 390 391 392
```


### getLowerKey

getLowerKey(key: K): K

Obtains the key that is placed in front of the input key in this container.

W
wusongqing 已提交
393 394
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
395 396 397 398 399 400 401 402 403 404 405 406
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Input key.|

**Return value**

| Type| Description|
| -------- | -------- |
| K | Key obtained.|

G
Gloria 已提交
407 408
**Error codes**

409
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
410 411 412 413 414

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getLowerKey method cannot be bound. |

W
wusongqing 已提交
415 416
**Example**

W
wusongqing 已提交
417
```ts
W
wusongqing 已提交
418
let treeMap = new TreeMap();
G
Gloria 已提交
419 420 421 422
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
treeMap.set("gander", 356);
let result = treeMap.getLowerKey("sparrow");
W
wusongqing 已提交
423 424 425 426 427 428 429 430 431
```


### getHigherKey

getHigherKey(key: K): K

Obtains the key that is placed next to the input key in this container.

W
wusongqing 已提交
432 433
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
434 435 436 437 438 439 440 441 442 443 444 445
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Input key.|

**Return value**

| Type| Description|
| -------- | -------- |
| K | Key obtained.|

G
Gloria 已提交
446 447
**Error codes**

448
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
449 450 451 452 453

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getHigherKey method cannot be bound. |

W
wusongqing 已提交
454 455
**Example**

W
wusongqing 已提交
456
```ts
W
wusongqing 已提交
457
let treeMap = new TreeMap();
G
Gloria 已提交
458 459 460 461
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
treeMap.set("gander", 356);
let result = treeMap.getHigherKey("sparrow");
W
wusongqing 已提交
462 463 464
```

### replace
W
wusongqing 已提交
465

W
wusongqing 已提交
466 467
replace(key: K, newValue: V): boolean

W
wusongqing 已提交
468 469 470
Replaces an element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
471 472 473 474 475

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
476 477
| key | K | Yes| Key of the target element.|
| newValue | V | Yes| New value of the element.|
W
wusongqing 已提交
478 479 480 481 482

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
483
| boolean | Returns **true** if the element is replaced successfully; returns **false** otherwise.|
W
wusongqing 已提交
484

G
Gloria 已提交
485 486
**Error codes**

487
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
488 489 490 491 492

| ID| Error Message|
| -------- | -------- |
| 10200011 | The replace method cannot be bound. |

W
wusongqing 已提交
493 494
**Example**

W
wusongqing 已提交
495
```ts
W
wusongqing 已提交
496
let treeMap = new TreeMap();
G
Gloria 已提交
497 498
treeMap.set("sparrow", 123);
let result = treeMap.replace("sparrow", 357);
W
wusongqing 已提交
499 500 501 502 503 504 505 506 507
```


### clear

clear(): void

Clears this container and sets its length to **0**.

W
wusongqing 已提交
508 509
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
510 511
**Error codes**

512
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
513 514 515 516 517

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

W
wusongqing 已提交
518 519
**Example**

W
wusongqing 已提交
520
```ts
W
wusongqing 已提交
521
let treeMap = new TreeMap();
G
Gloria 已提交
522 523
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
524 525 526 527 528 529 530 531 532 533
treeMap.clear();
```


### keys

keys(): IterableIterator&lt;K&gt;

Obtains an iterator that contains all the keys in this container.

W
wusongqing 已提交
534 535
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
536 537 538 539 540 541
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;K&gt; | Iterator obtained.|

G
Gloria 已提交
542 543
**Error codes**

544
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
545 546 547 548 549

| ID| Error Message|
| -------- | -------- |
| 10200011 | The keys method cannot be bound. |

W
wusongqing 已提交
550 551
**Example**

W
wusongqing 已提交
552
```ts
W
wusongqing 已提交
553
let treeMap = new TreeMap();
G
Gloria 已提交
554 555
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
556 557 558
let iter = treeMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
559
  console.log("value:" + temp);
W
wusongqing 已提交
560
  temp = iter.next().value;
G
Gloria 已提交
561
}
W
wusongqing 已提交
562 563 564 565 566 567 568 569 570
```


### values

values(): IterableIterator&lt;V&gt;

Obtains an iterator that contains all the values in this container.

W
wusongqing 已提交
571 572
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
573 574 575 576 577 578
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;V&gt; | Iterator obtained.|

G
Gloria 已提交
579 580
**Error codes**

581
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
582 583 584 585 586

| ID| Error Message|
| -------- | -------- |
| 10200011 | The values method cannot be bound. |

W
wusongqing 已提交
587 588
**Example**

W
wusongqing 已提交
589
```ts
W
wusongqing 已提交
590
let treeMap = new TreeMap();
G
Gloria 已提交
591 592
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
593 594 595
let iter = treeMap.values();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
596
  console.log("value:" + temp);
W
wusongqing 已提交
597 598 599 600 601 602 603
  temp = iter.next().value;
}
```


### forEach

604
forEach(callbackFn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void
W
wusongqing 已提交
605

W
wusongqing 已提交
606 607 608
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
609 610 611 612 613

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
614
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
G
Gloria 已提交
615
| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked.|
W
wusongqing 已提交
616 617 618 619

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
620 621
| value | V | No| Value of the element that is currently traversed.|
| key | K | No| Key of the element that is currently traversed.|
W
wusongqing 已提交
622 623
| map | TreeMap<K, V> | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
624 625
**Error codes**

626
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
627 628 629 630 631

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
632 633
**Example**

W
wusongqing 已提交
634
```ts
W
wusongqing 已提交
635
let treeMap = new TreeMap();
G
Gloria 已提交
636 637
treeMap.set("sparrow", 123);
treeMap.set("gull", 357);
W
wusongqing 已提交
638
treeMap.forEach((value, key) => {
G
Gloria 已提交
639
    console.log("value:" + value, "key:" + key);
W
wusongqing 已提交
640 641 642 643 644 645 646 647
});
```


### entries

entries(): IterableIterator<[K, V]>

W
wusongqing 已提交
648 649 650
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
651 652 653 654 655 656 657

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|

G
Gloria 已提交
658 659
**Error codes**

660
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
661 662 663 664 665

| ID| Error Message|
| -------- | -------- |
| 10200011 | The entries method cannot be bound. |

W
wusongqing 已提交
666 667
**Example**

W
wusongqing 已提交
668
```ts
W
wusongqing 已提交
669
let treeMap = new TreeMap();
G
Gloria 已提交
670 671
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
672 673 674
let iter = treeMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
675 676
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
677 678 679 680 681 682 683
  temp = iter.next().value;
}
```


### [Symbol.iterator]

W
wusongqing 已提交
684
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
W
wusongqing 已提交
685 686 687

Obtains an iterator, each item of which is a JavaScript object.

W
wusongqing 已提交
688 689
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
690 691 692 693 694
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|

G
Gloria 已提交
695 696
**Error codes**

697
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
698 699 700 701 702

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
703 704
**Example**

W
wusongqing 已提交
705
```ts
W
wusongqing 已提交
706
let treeMap = new TreeMap();
G
Gloria 已提交
707 708
treeMap.set("squirrel", 123);
treeMap.set("sparrow", 356);
W
wusongqing 已提交
709 710 711

// Method 1:
for (let item of treeMap) { 
W
wusongqing 已提交
712 713
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
W
wusongqing 已提交
714 715 716 717 718 719
}

// Method 2:
let iter = treeMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
720 721
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
722 723 724
  temp = iter.next().value;
}
```