1# 非线性容器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> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import TreeMap from '@ohos.util.TreeMap'; 19``` 20 21## TreeMap 22 23### 属性 24 25**系统能力:** SystemCapability.Utils.Lang 26 27| 名称 | 参数类型 | 可读 | 可写 | 说明 | 28| -------- | -------- | -------- | -------- | -------- | 29| length | number | 是 | 否 | TreeMap的元素个数。 | 30 31 32### constructor 33 34constructor(comparator?:(firstValue: K, secondValue: K) => boolean) 35 36TreeMap的构造函数。 37 38**系统能力:** SystemCapability.Utils.Lang 39 40**参数:** 41 42| 参数名 | 类型 | 必填 | 说明 | 43| -------- | -------- | -------- | -------- | 44| comparator | function | 否 | 用户自定义的比较函数。 | 45 46**示例:** 47 48```ts 49let treeMap = new TreeMap(); 50``` 51 52 53### isEmpty 54 55isEmpty(): boolean 56 57判断该容器是否为空。 58 59**系统能力:** SystemCapability.Utils.Lang 60 61**返回值:** 62 63| 类型 | 说明 | 64| -------- | -------- | 65| boolean | 为空返回true,否则返回false。 | 66 67**示例:** 68 69```ts 70const treeMap = new TreeMap(); 71let result = treeMap.isEmpty(); 72``` 73 74 75### hasKey 76 77hasKey(key: K): boolean 78 79判断此容器中是否含有该指定key。 80 81**系统能力:** SystemCapability.Utils.Lang 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| -------- | -------- | -------- | -------- | 87| key | K | 是 | 指定key | 88 89**返回值:** 90 91| 类型 | 说明 | 92| -------- | -------- | 93| boolean | 包含指定key返回true,否则返回false。 | 94 95**示例:** 96 97```ts 98let treeMap = new TreeMap(); 99let result = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 100treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 101let result1 = treeMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 102``` 103 104 105### hasValue 106 107hasValue(value: V): boolean 108 109判断此容器中是否含有该指定value。 110 111**系统能力:** SystemCapability.Utils.Lang 112 113**参数:** 114 115| 参数名 | 类型 | 必填 | 说明 | 116| -------- | -------- | -------- | -------- | 117| value | V | 是 | 指定value。 | 118 119**返回值:** 120 121| 类型 | 说明 | 122| -------- | -------- | 123| boolean | 包含指定元素返回true,否则返回false。 | 124 125**示例:** 126 127```ts 128let treeMap = new TreeMap(); 129let result = treeMap.hasValue(123); 130treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 131let result1 = treeMap.hasValue(123); 132``` 133 134 135### get 136 137get(key: K): V 138 139获取指定key所对应的value。 140 141**系统能力:** SystemCapability.Utils.Lang 142 143**参数:** 144 145| 参数名 | 类型 | 必填 | 说明 | 146| -------- | -------- | -------- | -------- | 147| key | K | 是 | 指定key。 | 148 149**返回值:** 150 151| 类型 | 说明 | 152| -------- | -------- | 153| V | 返回key映射的value值。 | 154 155**示例:** 156 157```ts 158let treeMap = new TreeMap(); 159treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 160treeMap.set("sdfs", 356); 161let result = treeMap.get("sdfs"); 162``` 163 164 165### getFirstKey 166 167getFirstKey(): K 168 169获取容器中排序第一的key。 170 171**系统能力:** SystemCapability.Utils.Lang 172 173**返回值:** 174 175| 类型 | 说明 | 176| -------- | -------- | 177| K | 返回排序第一的key。 | 178 179**示例:** 180 181```ts 182let treeMap = new TreeMap(); 183treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 184treeMap.set("sdfs", 356); 185let result = treeMap.getFirstKey(); 186``` 187 188 189### getLastKey 190 191getLastKey(): K 192 193获取容器中排序最后的key。 194 195**系统能力:** SystemCapability.Utils.Lang 196 197**返回值:** 198 199| 类型 | 说明 | 200| -------- | -------- | 201| K | 返回排序最后的key | 202 203**示例:** 204 205```ts 206let treeMap = new TreeMap(); 207treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 208treeMap.set("sdfs", 356); 209let result = treeMap.getLastKey(); 210``` 211 212 213### setAll 214 215setAll(map: TreeMap<K, V>): void 216 217将一个TreeMap中的所有元素组添加到另一个TreeMap中。 218 219**系统能力:** SystemCapability.Utils.Lang 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 224| -------- | -------- | -------- | -------- | 225| map | TreeMap<K, V> | 是 | 被添加元素的treeMap。 | 226 227**示例:** 228 229```ts 230let treeMap = new TreeMap(); 231treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 232treeMap.set("sdfs", 356); 233let map = new TreeMap(); 234treeMap.setAll(map); 235``` 236 237 238### set 239 240set(key: K, value: V): Object 241 242向容器中添加一组数据。 243 244**系统能力:** SystemCapability.Utils.Lang 245 246**参数:** 247 248| 参数名 | 类型 | 必填 | 说明 | 249| -------- | -------- | -------- | -------- | 250| key | K | 是 | 添加成员数据的键名。 | 251| value | V | 是 | 添加成员数据的值。 | 252 253**返回值:** 254 255| 类型 | 说明 | 256| -------- | -------- | 257| Object | 返回添加后的treeMap | 258 259**示例:** 260 261```ts 262let treeMap = new TreeMap(); 263treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 264``` 265 266 267### remove 268 269remove(key: K): V 270 271删除指定key对应的元素。 272 273**系统能力:** SystemCapability.Utils.Lang 274 275**参数:** 276 277| 参数名 | 类型 | 必填 | 说明 | 278| -------- | -------- | -------- | -------- | 279| key | K | 是 | 指定key。 | 280 281**返回值:** 282 283| 类型 | 说明 | 284| -------- | -------- | 285| V | 返回删除元素的值。 | 286 287**示例:** 288 289```ts 290let treeMap = new TreeMap(); 291treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 292treeMap.set("sdfs", 356); 293treeMap.remove("sdfs"); 294``` 295 296 297### getLowerKey 298 299getLowerKey(key: K): K 300 301获取容器中比传入key排序靠前一位的key。 302 303**系统能力:** SystemCapability.Utils.Lang 304 305**参数:** 306 307| 参数名 | 类型 | 必填 | 说明 | 308| -------- | -------- | -------- | -------- | 309| key | K | 是 | 对比的key值。 | 310 311**返回值:** 312 313| 类型 | 说明 | 314| -------- | -------- | 315| K | 返回排序中key前一位的数据。 | 316 317**示例:** 318 319```ts 320let treeMap = new TreeMap(); 321treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 322treeMap.set("sdfs", 356); 323treeMap.set("zdfgsd", 356); 324let result = treeMap.getLowerKey("sdfs"); 325``` 326 327 328### getHigherKey 329 330getHigherKey(key: K): K 331 332获取容器中比传入key排序靠后一位的key。 333 334**系统能力:** SystemCapability.Utils.Lang 335 336**参数:** 337 338| 参数名 | 类型 | 必填 | 说明 | 339| -------- | -------- | -------- | -------- | 340| key | K | 是 | 对比的key值。 | 341 342**返回值:** 343 344| 类型 | 说明 | 345| -------- | -------- | 346| K | 返回排序中key后一位的数据。 | 347 348**示例:** 349 350```ts 351let treeMap = new TreeMap(); 352treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 353treeMap.set("sdfs", 356); 354treeMap.set("zdfgsd", 356); 355let result = treeMap.getHigherKey("sdfs"); 356``` 357 358### replace 359 360replace(key: K, newValue: V): boolean 361 362对容器中一组数据进行更新(替换)。 363 364**系统能力:** SystemCapability.Utils.Lang 365 366**参数:** 367 368| 参数名 | 类型 | 必填 | 说明 | 369| -------- | -------- | -------- | -------- | 370| key | K | 是 | 指定key。 | 371| newValue | V | 是 | 替换的元素。 | 372 373**返回值:** 374 375| 类型 | 说明 | 376| -------- | -------- | 377| boolean | 对指定key对应的元素替换成功返回true,否则返回false。 | 378 379**示例:** 380 381```ts 382let treeMap = new TreeMap(); 383treeMap.set("sdfs", 123); 384let result = treeMap.replace("sdfs", 357); 385``` 386 387 388### clear 389 390clear(): void 391 392清除容器中的所有元素,并把length置为0。 393 394**系统能力:** SystemCapability.Utils.Lang 395 396**示例:** 397 398```ts 399let treeMap = new TreeMap(); 400treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 401treeMap.set("sdfs", 356); 402treeMap.clear(); 403``` 404 405 406### keys 407 408keys(): IterableIterator<K> 409 410返回包含此映射中包含的键的新迭代器对象。 411 412**系统能力:** SystemCapability.Utils.Lang 413 414**返回值:** 415 416| 类型 | 说明 | 417| -------- | -------- | 418| IterableIterator<K> | 返回一个迭代器。 | 419 420**示例:** 421 422```ts 423let treeMap = new TreeMap(); 424treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 425treeMap.set("sdfs", 356); 426let iter = treeMap.keys(); 427let temp = iter.next().value; 428while(temp != undefined) { 429 console.log("value:" + temp); 430 temp = iter.next().value; 431} 432``` 433 434 435### values 436 437values(): IterableIterator<V> 438 439返回包含此映射中键值对的新迭代器对象。 440 441**系统能力:** SystemCapability.Utils.Lang 442 443**返回值:** 444 445| 类型 | 说明 | 446| -------- | -------- | 447| IterableIterator<V> | 返回一个迭代器。 | 448 449**示例:** 450 451```ts 452let treeMap = new TreeMap(); 453treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 454treeMap.set("sdfs", 356); 455let iter = treeMap.values(); 456let temp = iter.next().value; 457while(temp != undefined) { 458 console.log("value:" + temp); 459 temp = iter.next().value; 460} 461``` 462 463 464### forEach 465 466forEach(callbackfn: (value?: V, key?: K, map?: TreeMap<K, V>) => void, thisArg?: Object): void 467 468通过回调函数来遍历实例对象上的元素以及元素对应的下标。 469 470**系统能力:** SystemCapability.Utils.Lang 471 472**参数:** 473 474| 参数名 | 类型 | 必填 | 说明 | 475| -------- | -------- | -------- | -------- | 476| callbackfn | function | 是 | 回调函数。 | 477| thisArg | Object | 否 | callbackfn被调用时用作this值。 | 478 479callbackfn的参数说明: 480| 参数名 | 类型 | 必填 | 说明 | 481| -------- | -------- | -------- | -------- | 482| value | V | 否 | 当前遍历到的元素键值对的值。 | 483| key | K | 否 | 当前遍历到的元素键值对的键。 | 484| map | TreeMap<K, V> | 否 | 当前调用forEach方法的实例对象。 | 485 486**示例:** 487 488```ts 489let treeMap = new TreeMap(); 490treeMap.set("sdfs", 123); 491treeMap.set("dfsghsf", 357); 492treeMap.forEach((value, key) => { 493 console.log("value:" + value, "key:" + key); 494}); 495``` 496 497 498### entries 499 500entries(): IterableIterator<[K, V]> 501 502返回包含此映射中键值对的新迭代器对象。 503 504**系统能力:** SystemCapability.Utils.Lang 505 506**返回值:** 507 508| 类型 | 说明 | 509| -------- | -------- | 510| IterableIterator<[K, V]> | 返回一个迭代器。 | 511 512**示例:** 513 514```ts 515let treeMap = new TreeMap(); 516treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 517treeMap.set("sdfs", 356); 518let iter = treeMap.entries(); 519let temp = iter.next().value; 520while(temp != undefined) { 521 console.log("key:" + temp[0]); 522 console.log("value:" + temp[1]); 523 temp = iter.next().value; 524} 525``` 526 527 528### [Symbol.iterator] 529 530[Symbol.iterator]\(): IterableIterator<[K, V]> 531 532返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 533 534**系统能力:** SystemCapability.Utils.Lang 535 536**返回值:** 537| 类型 | 说明 | 538| -------- | -------- | 539| IterableIterator<[K, V]> | 返回一个迭代器。 | 540 541**示例:** 542 543```ts 544let treeMap = new TreeMap(); 545treeMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 546treeMap.set("sdfs", 356); 547 548// 使用方法一: 549for (let item of treeMap) { 550 console.log("key:" + item[0]); 551 console.log("value:" + item[1]); 552} 553 554// 使用方法二: 555let iter = treeMap[Symbol.iterator](); 556let temp = iter.next().value; 557while(temp != undefined) { 558 console.log("key:" + temp[0]); 559 console.log("value:" + temp[1]); 560 temp = iter.next().value; 561} 562```