js-apis-treemap.md 11.4 KB
Newer Older
W
wusongqing 已提交
1 2
# Nonlinear Container TreeMap 

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6
> 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 已提交
7 8 9 10 11 12 13
**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 已提交
14 15 16

## Modules to Import

W
wusongqing 已提交
17 18
```ts
import TreeMap from '@ohos.util.TreeMap';  
W
wusongqing 已提交
19 20 21 22 23 24
```

## TreeMap

### Attributes

W
wusongqing 已提交
25 26
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
27 28
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
29
| length | number | Yes| No| Number of elements in a tree map (called container later).|
W
wusongqing 已提交
30 31 32 33 34 35 36 37


### constructor

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

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

W
wusongqing 已提交
38 39
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
40 41 42 43 44 45 46 47
**Parameters**

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

**Example**

W
wusongqing 已提交
48
```ts
W
wusongqing 已提交
49 50 51 52 53 54
let treeMap = new TreeMap();
```


### isEmpty

Z
zengyawen 已提交
55
isEmpty(): boolean
W
wusongqing 已提交
56

W
wusongqing 已提交
57 58 59
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
60 61 62 63 64 65 66 67 68

**Return value**

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

**Example**

W
wusongqing 已提交
69
```ts
W
wusongqing 已提交
70 71 72 73 74 75 76
const treeMap = new TreeMap();
let result = treeMap.isEmpty();
```


### hasKey

Z
zengyawen 已提交
77
hasKey(key: K): boolean
W
wusongqing 已提交
78 79 80

Checks whether this container has the specified key.

W
wusongqing 已提交
81 82
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
83 84 85 86
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
87
| key | K | Yes| Target key.|
W
wusongqing 已提交
88 89 90 91 92 93 94 95 96

**Return value**

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

**Example**

W
wusongqing 已提交
97
```ts
W
wusongqing 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
let treeMap = new TreeMap();
let result = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```


### hasValue

hasValue(value: V): boolean

Checks whether this container has the specified value.

W
wusongqing 已提交
111 112
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
113 114 115 116
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
117
| value | V | Yes| Target value.|
W
wusongqing 已提交
118 119 120 121 122 123 124 125 126

**Return value**

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

**Example**

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


### get

get(key: K): V

Obtains the value of the specified key in this container.

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

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

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
147
| key | K | Yes| Target key.|
W
wusongqing 已提交
148 149 150 151 152 153 154 155 156

**Return value**

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

**Example**

W
wusongqing 已提交
157
```ts
W
wusongqing 已提交
158 159 160 161 162 163 164 165 166
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.get("sdfs");
```


### getFirstKey

Z
zengyawen 已提交
167
getFirstKey(): K
W
wusongqing 已提交
168 169 170

Obtains the first key in this container.

W
wusongqing 已提交
171 172
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
173 174 175 176 177 178 179 180
**Return value**

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

**Example**

W
wusongqing 已提交
181
```ts
W
wusongqing 已提交
182 183 184 185 186 187 188 189 190
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.getFirstKey();
```


### getLastKey

Z
zengyawen 已提交
191
getLastKey(): K
W
wusongqing 已提交
192 193 194

Obtains the last key in this container.

W
wusongqing 已提交
195 196
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
197 198 199 200 201 202 203 204
**Return value**

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

**Example**

W
wusongqing 已提交
205
```ts
W
wusongqing 已提交
206 207 208 209 210 211 212 213 214 215 216
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let result = treeMap.getLastKey();
```


### setAll

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

W
wusongqing 已提交
217 218 219
Adds all elements in a **TreeMap** instance to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
220 221 222 223 224

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
225
| map | TreeMap<K, V> | Yes| **TreeMap** instance whose elements are to be added to the current container.|
W
wusongqing 已提交
226 227 228

**Example**

W
wusongqing 已提交
229
```ts
W
wusongqing 已提交
230 231 232 233 234 235 236 237 238
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let map = new TreeMap();
treeMap.setAll(map);
```


### set
Z
zengyawen 已提交
239

W
wusongqing 已提交
240 241
set(key: K, value: V): Object

W
wusongqing 已提交
242 243 244
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
245 246 247 248 249

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
250 251
| key | K | Yes| Key of the target element.|
| value | V | Yes| Value of the target element.|
W
wusongqing 已提交
252 253 254 255 256

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
257
| Object | Container that contains the new element.|
W
wusongqing 已提交
258 259 260

**Example**

W
wusongqing 已提交
261
```ts
W
wusongqing 已提交
262 263 264 265 266 267 268
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
```


### remove

Z
zengyawen 已提交
269
remove(key: K): V
W
wusongqing 已提交
270

W
wusongqing 已提交
271 272 273
Removes the element with the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
274 275 276 277 278

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
279
| key | K | Yes| Target key.|
W
wusongqing 已提交
280 281 282 283 284

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
285
| V | Value of the element removed.|
W
wusongqing 已提交
286 287 288

**Example**

W
wusongqing 已提交
289
```ts
W
wusongqing 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.remove("sdfs");
```


### getLowerKey

getLowerKey(key: K): K

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

W
wusongqing 已提交
303 304
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318
**Parameters**

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

**Return value**

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

**Example**

W
wusongqing 已提交
319
```ts
W
wusongqing 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.set("zdfgsd", 356);
let result = treeMap.getLowerKey("sdfs");
```


### getHigherKey

getHigherKey(key: K): K

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

W
wusongqing 已提交
334 335
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349
**Parameters**

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

**Return value**

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

**Example**

W
wusongqing 已提交
350
```ts
W
wusongqing 已提交
351 352 353 354 355 356 357 358
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.set("zdfgsd", 356);
let result = treeMap.getHigherKey("sdfs");
```

### replace
Z
zengyawen 已提交
359

W
wusongqing 已提交
360 361
replace(key: K, newValue: V): boolean

W
wusongqing 已提交
362 363 364
Replaces an element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
365 366 367 368 369

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
370 371
| key | K | Yes| Key of the target element.|
| newValue | V | Yes| New value of the element.|
W
wusongqing 已提交
372 373 374 375 376

**Return value**

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

**Example**

W
wusongqing 已提交
381
```ts
W
wusongqing 已提交
382 383 384 385 386 387 388 389 390 391 392 393
let treeMap = new TreeMap();
treeMap.set("sdfs", 123);
let result = treeMap.replace("sdfs", 357);
```


### clear

clear(): void

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

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

W
wusongqing 已提交
396 397
**Example**

W
wusongqing 已提交
398
```ts
W
wusongqing 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
treeMap.clear();
```


### keys

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

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

W
wusongqing 已提交
412 413
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
414 415 416 417 418 419 420 421
**Return value**

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

**Example**

W
wusongqing 已提交
422
```ts
W
wusongqing 已提交
423 424 425 426 427 428
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
429
  console.log("value:" + temp);
W
wusongqing 已提交
430 431 432 433 434 435 436 437 438 439 440
  temp = iter.next().value;
} 
```


### values

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

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

W
wusongqing 已提交
441 442
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
443 444 445 446 447 448 449 450
**Return value**

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

**Example**

W
wusongqing 已提交
451
```ts
W
wusongqing 已提交
452 453 454 455 456 457
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.values();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
458
  console.log("value:" + temp);
W
wusongqing 已提交
459 460 461 462 463 464 465
  temp = iter.next().value;
}
```


### forEach

Z
zengyawen 已提交
466
forEach(callbackfn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void
W
wusongqing 已提交
467

W
wusongqing 已提交
468 469 470
Uses a callback to traverse the elements in this container and obtain their position indexes.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
476
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
477 478 479 480 481
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
482 483
| value | V | No| Value of the element that is currently traversed.|
| key | K | No| Key of the element that is currently traversed.|
W
wusongqing 已提交
484 485 486 487
| map | TreeMap<K, V> | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
488
```ts
W
wusongqing 已提交
489 490 491 492
let treeMap = new TreeMap();
treeMap.set("sdfs", 123);
treeMap.set("dfsghsf", 357);
treeMap.forEach((value, key) => {
W
wusongqing 已提交
493
  console.log("value:" + value, key);
W
wusongqing 已提交
494 495 496 497 498 499 500 501
});
```


### entries

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

W
wusongqing 已提交
502 503 504
Obtains an iterator that contains all the elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
505 506 507 508 509 510 511 512 513

**Return value**

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

**Example**

W
wusongqing 已提交
514
```ts
W
wusongqing 已提交
515 516 517 518 519 520
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);
let iter = treeMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
521 522
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
523 524 525 526 527 528 529
  temp = iter.next().value;
}
```


### [Symbol.iterator]

Z
zengyawen 已提交
530
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
W
wusongqing 已提交
531 532 533

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

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

W
wusongqing 已提交
536 537 538 539 540 541 542
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|

**Example**

W
wusongqing 已提交
543
```ts
W
wusongqing 已提交
544 545 546 547 548 549
let treeMap = new TreeMap();
treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
treeMap.set("sdfs", 356);

// Method 1:
for (let item of treeMap) { 
W
wusongqing 已提交
550 551
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
W
wusongqing 已提交
552 553 554 555 556 557
}

// Method 2:
let iter = treeMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
558 559
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
560 561 562
  temp = iter.next().value;
}
```