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