1# @ohos.util.TreeMap (非线性容器TreeMap) 2 3TreeMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。 4 5TreeMap底层使用红黑树实现,可以利用二叉树特性快速查找键值对。key值有序存储,可以实现快速的插入和删除。 6 7TreeMap和[HashMap](js-apis-treemap.md)相比,HashMap依据键的hashCode存取数据,访问速度较快。而TreeMap是有序存取,效率较低。 8 9**推荐使用场景:** 一般需要存储有序键值对的场景,可以使用TreeMap。 10 11文档中存在泛型的使用,涉及以下泛型标记符: 12 13- K: Key, 键 14 15- V: Value, 值 16 17> **说明:** 18> 19> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 20 21## 导入模块 22 23```ts 24import TreeMap from '@ohos.util.TreeMap'; 25``` 26 27## TreeMap 28 29### 属性 30 31**系统能力:** SystemCapability.Utils.Lang 32 33| 名称 | 类型 | 可读 | 可写 | 说明 | 34| -------- | -------- | -------- | -------- | -------- | 35| length | number | 是 | 否 | TreeMap的元素个数。 | 36 37 38### constructor 39 40constructor(comparator?:(firstValue: K, secondValue: K) => boolean) 41 42TreeMap的构造函数。 43 44**系统能力:** SystemCapability.Utils.Lang 45 46**参数:** 47 48| 参数名 | 类型 | 必填 | 说明 | 49| -------- | -------- | -------- | -------- | 50| comparator | function | 否 | 用户自定义的比较函数。 | 51 52**错误码:** 53 54以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 55 56| 错误码ID | 错误信息 | 57| -------- | -------- | 58| 10200012 | The TreeMap's constructor cannot be directly invoked. | 59 60**示例:** 61 62```ts 63let treeMap = new TreeMap(); 64``` 65 66 67### isEmpty 68 69isEmpty(): boolean 70 71判断该容器是否为空。 72 73**系统能力:** SystemCapability.Utils.Lang 74 75**返回值:** 76 77| 类型 | 说明 | 78| -------- | -------- | 79| boolean | 为空返回true,否则返回false。 | 80 81**错误码:** 82 83以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 84 85| 错误码ID | 错误信息 | 86| -------- | -------- | 87| 10200011 | The isEmpty method cannot be bound. | 88 89**示例:** 90 91```ts 92const treeMap = new TreeMap(); 93let result = treeMap.isEmpty(); 94``` 95 96 97### hasKey 98 99hasKey(key: K): boolean 100 101判断此容器中是否含有该指定key。 102 103**系统能力:** SystemCapability.Utils.Lang 104 105**参数:** 106 107| 参数名 | 类型 | 必填 | 说明 | 108| -------- | -------- | -------- | -------- | 109| key | K | 是 | 指定key | 110 111**返回值:** 112 113| 类型 | 说明 | 114| -------- | -------- | 115| boolean | 包含指定key返回true,否则返回false。 | 116 117**错误码:** 118 119以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 120 121| 错误码ID | 错误信息 | 122| -------- | -------- | 123| 10200011 | The hasKey method cannot be bound. | 124 125**示例:** 126 127```ts 128let treeMap = new TreeMap(); 129let result = treeMap.hasKey("squirrel"); 130treeMap.set("squirrel", 123); 131let result1 = treeMap.hasKey("squirrel"); 132``` 133 134 135### hasValue 136 137hasValue(value: V): boolean 138 139判断此容器中是否含有该指定value。 140 141**系统能力:** SystemCapability.Utils.Lang 142 143**参数:** 144 145| 参数名 | 类型 | 必填 | 说明 | 146| -------- | -------- | -------- | -------- | 147| value | V | 是 | 指定value。 | 148 149**返回值:** 150 151| 类型 | 说明 | 152| -------- | -------- | 153| boolean | 包含指定元素返回true,否则返回false。 | 154 155**错误码:** 156 157以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 158 159| 错误码ID | 错误信息 | 160| -------- | -------- | 161| 10200011 | The hasValue method cannot be bound. | 162 163**示例:** 164 165```ts 166let treeMap = new TreeMap(); 167let result = treeMap.hasValue(123); 168treeMap.set("squirrel", 123); 169let result1 = treeMap.hasValue(123); 170``` 171 172 173### get 174 175get(key: K): V 176 177获取指定key所对应的value。 178 179**系统能力:** SystemCapability.Utils.Lang 180 181**参数:** 182 183| 参数名 | 类型 | 必填 | 说明 | 184| -------- | -------- | -------- | -------- | 185| key | K | 是 | 指定key。 | 186 187**返回值:** 188 189| 类型 | 说明 | 190| -------- | -------- | 191| V | 返回key映射的value值。 | 192 193**错误码:** 194 195以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 196 197| 错误码ID | 错误信息 | 198| -------- | -------- | 199| 10200011 | The get method cannot be bound. | 200 201**示例:** 202 203```ts 204let treeMap = new TreeMap(); 205treeMap.set("squirrel", 123); 206treeMap.set("sparrow", 356); 207let result = treeMap.get("sparrow"); 208``` 209 210 211### getFirstKey 212 213getFirstKey(): K 214 215获取容器中排序第一的key。 216 217**系统能力:** SystemCapability.Utils.Lang 218 219**返回值:** 220 221| 类型 | 说明 | 222| -------- | -------- | 223| K | 返回排序第一的key。 | 224 225**错误码:** 226 227以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 228 229| 错误码ID | 错误信息 | 230| -------- | -------- | 231| 10200011 | The getFirstKey method cannot be bound. | 232 233**示例:** 234 235```ts 236let treeMap = new TreeMap(); 237treeMap.set("squirrel", 123); 238treeMap.set("sparrow", 356); 239let result = treeMap.getFirstKey(); 240``` 241 242 243### getLastKey 244 245getLastKey(): K 246 247获取容器中排序最后的key。 248 249**系统能力:** SystemCapability.Utils.Lang 250 251**返回值:** 252 253| 类型 | 说明 | 254| -------- | -------- | 255| K | 返回排序最后的key | 256 257**错误码:** 258 259以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 260 261| 错误码ID | 错误信息 | 262| -------- | -------- | 263| 10200011 | The getLastKey method cannot be bound. | 264 265**示例:** 266 267```ts 268let treeMap = new TreeMap(); 269treeMap.set("squirrel", 123); 270treeMap.set("sparrow", 356); 271let result = treeMap.getLastKey(); 272``` 273 274 275### setAll 276 277setAll(map: TreeMap<K, V>): void 278 279将一个TreeMap中的所有元素组添加到另一个TreeMap中。 280 281**系统能力:** SystemCapability.Utils.Lang 282 283**参数:** 284 285| 参数名 | 类型 | 必填 | 说明 | 286| -------- | -------- | -------- | -------- | 287| map | TreeMap<K, V> | 是 | 该map会添加到其调用setAll接口的map对象中。 | 288 289**错误码:** 290 291以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 292 293| 错误码ID | 错误信息 | 294| -------- | -------- | 295| 10200011 | The setAll method cannot be bound. | 296 297**示例:** 298 299```ts 300let treeMap = new TreeMap(); 301treeMap.set("squirrel", 123); 302treeMap.set("sparrow", 356); 303let map = new TreeMap(); 304map.set("demo", 12); 305map.setAll(treeMap); // 将treeMap中的所有元素添加到map中 306map.forEach((value, key) => { 307 console.log("test" + value, key); // 打印结果 12 demo、356 sparrow、123 squirrel 308}) 309``` 310 311 312### set 313 314set(key: K, value: V): Object 315 316向容器中添加一组数据。 317 318**系统能力:** SystemCapability.Utils.Lang 319 320**参数:** 321 322| 参数名 | 类型 | 必填 | 说明 | 323| -------- | -------- | -------- | -------- | 324| key | K | 是 | 添加成员数据的键名。 | 325| value | V | 是 | 添加成员数据的值。 | 326 327**返回值:** 328 329| 类型 | 说明 | 330| -------- | -------- | 331| Object | 返回添加后的treeMap | 332 333**错误码:** 334 335以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 336 337| 错误码ID | 错误信息 | 338| -------- | -------- | 339| 10200011 | The set method cannot be bound. | 340 341**示例:** 342 343```ts 344let treeMap = new TreeMap(); 345treeMap.set("squirrel", 123); 346``` 347 348 349### remove 350 351remove(key: K): V 352 353删除指定key对应的元素。 354 355**系统能力:** SystemCapability.Utils.Lang 356 357**参数:** 358 359| 参数名 | 类型 | 必填 | 说明 | 360| -------- | -------- | -------- | -------- | 361| key | K | 是 | 指定key。 | 362 363**返回值:** 364 365| 类型 | 说明 | 366| -------- | -------- | 367| V | 返回删除元素的值。 | 368 369**错误码:** 370 371以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 372 373| 错误码ID | 错误信息 | 374| -------- | -------- | 375| 10200011 | The remove method cannot be bound. | 376 377**示例:** 378 379```ts 380let treeMap = new TreeMap(); 381treeMap.set("squirrel", 123); 382treeMap.set("sparrow", 356); 383treeMap.remove("sparrow"); 384``` 385 386 387### getLowerKey 388 389getLowerKey(key: K): K 390 391获取容器中比传入key排序靠前一位的key。 392 393**系统能力:** SystemCapability.Utils.Lang 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | -------- | -------- | -------- | 399| key | K | 是 | 对比的key值。 | 400 401**返回值:** 402 403| 类型 | 说明 | 404| -------- | -------- | 405| K | 返回排序中key前一位的数据。 | 406 407**错误码:** 408 409以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 410 411| 错误码ID | 错误信息 | 412| -------- | -------- | 413| 10200011 | The getLowerKey method cannot be bound. | 414 415**示例:** 416 417```ts 418let treeMap = new TreeMap(); 419treeMap.set("squirrel", 123); 420treeMap.set("sparrow", 356); 421treeMap.set("gander", 356); 422let result = treeMap.getLowerKey("sparrow"); 423``` 424 425 426### getHigherKey 427 428getHigherKey(key: K): K 429 430获取容器中比传入key排序靠后一位的key。 431 432**系统能力:** SystemCapability.Utils.Lang 433 434**参数:** 435 436| 参数名 | 类型 | 必填 | 说明 | 437| -------- | -------- | -------- | -------- | 438| key | K | 是 | 对比的key值。 | 439 440**返回值:** 441 442| 类型 | 说明 | 443| -------- | -------- | 444| K | 返回排序中key后一位的数据。 | 445 446**错误码:** 447 448以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 449 450| 错误码ID | 错误信息 | 451| -------- | -------- | 452| 10200011 | The getHigherKey method cannot be bound. | 453 454**示例:** 455 456```ts 457let treeMap = new TreeMap(); 458treeMap.set("squirrel", 123); 459treeMap.set("sparrow", 356); 460treeMap.set("gander", 356); 461let result = treeMap.getHigherKey("sparrow"); 462``` 463 464### replace 465 466replace(key: K, newValue: V): boolean 467 468对容器中一组数据进行更新(替换)。 469 470**系统能力:** SystemCapability.Utils.Lang 471 472**参数:** 473 474| 参数名 | 类型 | 必填 | 说明 | 475| -------- | -------- | -------- | -------- | 476| key | K | 是 | 指定key。 | 477| newValue | V | 是 | 替换的元素。 | 478 479**返回值:** 480 481| 类型 | 说明 | 482| -------- | -------- | 483| boolean | 对指定key对应的元素替换成功返回true,否则返回false。 | 484 485**错误码:** 486 487以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 488 489| 错误码ID | 错误信息 | 490| -------- | -------- | 491| 10200011 | The replace method cannot be bound. | 492 493**示例:** 494 495```ts 496let treeMap = new TreeMap(); 497treeMap.set("sparrow", 123); 498let result = treeMap.replace("sparrow", 357); 499``` 500 501 502### clear 503 504clear(): void 505 506清除容器中的所有元素,并把length置为0。 507 508**系统能力:** SystemCapability.Utils.Lang 509 510**错误码:** 511 512以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 513 514| 错误码ID | 错误信息 | 515| -------- | -------- | 516| 10200011 | The clear method cannot be bound. | 517 518**示例:** 519 520```ts 521let treeMap = new TreeMap(); 522treeMap.set("squirrel", 123); 523treeMap.set("sparrow", 356); 524treeMap.clear(); 525``` 526 527 528### keys 529 530keys(): IterableIterator<K> 531 532返回包含此映射中包含的键的新迭代器对象。 533 534**系统能力:** SystemCapability.Utils.Lang 535 536**返回值:** 537 538| 类型 | 说明 | 539| -------- | -------- | 540| IterableIterator<K> | 返回一个迭代器。 | 541 542**错误码:** 543 544以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 545 546| 错误码ID | 错误信息 | 547| -------- | -------- | 548| 10200011 | The keys method cannot be bound. | 549 550**示例:** 551 552```ts 553let treeMap = new TreeMap(); 554treeMap.set("squirrel", 123); 555treeMap.set("sparrow", 356); 556let iter = treeMap.keys(); 557let temp = iter.next().value; 558while(temp != undefined) { 559 console.log("value:" + temp); 560 temp = iter.next().value; 561} 562``` 563 564 565### values 566 567values(): IterableIterator<V> 568 569返回包含此映射中键值的新迭代器对象。 570 571**系统能力:** SystemCapability.Utils.Lang 572 573**返回值:** 574 575| 类型 | 说明 | 576| -------- | -------- | 577| IterableIterator<V> | 返回一个迭代器。 | 578 579**错误码:** 580 581以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 582 583| 错误码ID | 错误信息 | 584| -------- | -------- | 585| 10200011 | The values method cannot be bound. | 586 587**示例:** 588 589```ts 590let treeMap = new TreeMap(); 591treeMap.set("squirrel", 123); 592treeMap.set("sparrow", 356); 593let iter = treeMap.values(); 594let temp = iter.next().value; 595while(temp != undefined) { 596 console.log("value:" + temp); 597 temp = iter.next().value; 598} 599``` 600 601 602### forEach 603 604forEach(callbackFn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void 605 606通过回调函数来遍历实例对象上的元素以及元素对应的下标。 607 608**系统能力:** SystemCapability.Utils.Lang 609 610**参数:** 611 612| 参数名 | 类型 | 必填 | 说明 | 613| -------- | -------- | -------- | -------- | 614| callbackFn | function | 是 | 回调函数。 | 615| thisArg | Object | 否 | callbackFn被调用时用作this值。 | 616 617callbackFn的参数说明: 618| 参数名 | 类型 | 必填 | 说明 | 619| -------- | -------- | -------- | -------- | 620| value | V | 否 | 当前遍历到的元素键值对的值。 | 621| key | K | 否 | 当前遍历到的元素键值对的键。 | 622| map | TreeMap<K, V> | 否 | 当前调用forEach方法的实例对象。 | 623 624**错误码:** 625 626以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 627 628| 错误码ID | 错误信息 | 629| -------- | -------- | 630| 10200011 | The forEach method cannot be bound. | 631 632**示例:** 633 634```ts 635let treeMap = new TreeMap(); 636treeMap.set("sparrow", 123); 637treeMap.set("gull", 357); 638treeMap.forEach((value, key) => { 639 console.log("value:" + value, "key:" + key); 640}); 641``` 642 643 644### entries 645 646entries(): IterableIterator<[K, V]> 647 648返回包含此映射中键值对的新迭代器对象。 649 650**系统能力:** SystemCapability.Utils.Lang 651 652**返回值:** 653 654| 类型 | 说明 | 655| -------- | -------- | 656| IterableIterator<[K, V]> | 返回一个迭代器。 | 657 658**错误码:** 659 660以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 661 662| 错误码ID | 错误信息 | 663| -------- | -------- | 664| 10200011 | The entries method cannot be bound. | 665 666**示例:** 667 668```ts 669let treeMap = new TreeMap(); 670treeMap.set("squirrel", 123); 671treeMap.set("sparrow", 356); 672let iter = treeMap.entries(); 673let temp = iter.next().value; 674while(temp != undefined) { 675 console.log("key:" + temp[0]); 676 console.log("value:" + temp[1]); 677 temp = iter.next().value; 678} 679``` 680 681 682### [Symbol.iterator] 683 684[Symbol.iterator]\(): IterableIterator<[K, V]> 685 686返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 687 688**系统能力:** SystemCapability.Utils.Lang 689 690**返回值:** 691| 类型 | 说明 | 692| -------- | -------- | 693| IterableIterator<[K, V]> | 返回一个迭代器。 | 694 695**错误码:** 696 697以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 698 699| 错误码ID | 错误信息 | 700| -------- | -------- | 701| 10200011 | The Symbol.iterator method cannot be bound. | 702 703**示例:** 704 705```ts 706let treeMap = new TreeMap(); 707treeMap.set("squirrel", 123); 708treeMap.set("sparrow", 356); 709 710// 使用方法一: 711for (let item of treeMap) { 712 console.log("key:" + item[0]); 713 console.log("value:" + item[1]); 714} 715 716// 使用方法二: 717let iter = treeMap[Symbol.iterator](); 718let temp = iter.next().value; 719while(temp != undefined) { 720 console.log("key:" + temp[0]); 721 console.log("value:" + temp[1]); 722 temp = iter.next().value; 723} 724```