1# @ohos.util.HashMap (Nonlinear Container HashMap) 2 3**HashMap** is a map implemented based on the array, linked list, and red-black tree. It provides efficient data query, insertion, and removal. The elements in a **HashMap** instance are mappings of key-value pairs. Each key must be unique and have only one value. 4 5**HashMap** is faster in accessing data than **[TreeMap](js-apis-treemap.md)**, because the former accesses the keys based on the hash codes, whereas the latter stores and accesses the keys in sorted order. 6 7**[HashSet](js-apis-hashset.md)** is implemented based on **HashMap**. The input parameter of **HashMap** consists of **key** and **value**. In **HashSet**, only the **value** object is processed. 8 9**Recommended use case**: Use **HashMap** when you need to quickly access, remove, and insert key-value pairs. 10 11This topic uses the following to identify the use of generics: 12- K: Key 13- V: Value 14 15> **NOTE** 16> 17> 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. 18 19 20## Modules to Import 21 22```ts 23import { HashMap } from '@kit.ArkTS'; 24``` 25 26## HashMap 27 28### Attributes 29 30**Atomic service API**: This API can be used in atomic services since API version 12. 31 32**System capability**: SystemCapability.Utils.Lang 33 34| Name| Type| Readable| Writable| Description| 35| -------- | -------- | -------- | -------- | -------- | 36| length | number | Yes| No| Number of elements in a hash map (called container later).| 37 38 39### constructor 40 41constructor() 42 43A constructor used to create a **HashMap** instance. 44 45**Atomic service API**: This API can be used in atomic services since API version 12. 46 47**System capability**: SystemCapability.Utils.Lang 48 49**Error codes** 50 51For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 52 53| ID| Error Message| 54| -------- | -------- | 55| 10200012 | The HashMap's constructor cannot be directly invoked. | 56 57**Example** 58 59```ts 60let hashMap: HashMap<string, number> = new HashMap(); 61``` 62 63 64### isEmpty 65 66isEmpty(): boolean 67 68Checks whether this container is empty (contains no element). 69 70**Atomic service API**: This API can be used in atomic services since API version 12. 71 72**System capability**: SystemCapability.Utils.Lang 73 74**Return value** 75 76| Type| Description| 77| -------- | -------- | 78| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 79 80**Error codes** 81 82For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 83 84| ID| Error Message| 85| -------- | -------- | 86| 10200011 | The isEmpty method cannot be bound. | 87 88**Example** 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 100Checks whether this container contains the specified key. 101 102**Atomic service API**: This API can be used in atomic services since API version 12. 103 104**System capability**: SystemCapability.Utils.Lang 105 106**Parameters** 107 108| Name| Type| Mandatory| Description| 109| -------- | -------- | -------- | -------- | 110| key | K | Yes| Target key.| 111 112**Return value** 113 114| Type| Description| 115| -------- | -------- | 116| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.| 117 118**Error codes** 119 120For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 121 122| ID| Error Message| 123| -------- | -------- | 124| 10200011 | The hasKey method cannot be bound. | 125 126**Example** 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 139Checks whether this container contains the specified value. 140 141**Atomic service API**: This API can be used in atomic services since API version 12. 142 143**System capability**: SystemCapability.Utils.Lang 144 145**Parameters** 146 147| Name| Type| Mandatory| Description| 148| -------- | -------- | -------- | -------- | 149| value | V | Yes| Target value.| 150 151**Return value** 152 153| Type| Description| 154| -------- | -------- | 155| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.| 156 157**Error codes** 158 159For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 160 161| ID| Error Message| 162| -------- | -------- | 163| 10200011 | The hasValue method cannot be bound. | 164 165**Example** 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 178Obtains the value of the specified key in this container. If nothing is obtained, **undefined** is returned. 179 180**Atomic service API**: This API can be used in atomic services since API version 12. 181 182**System capability**: SystemCapability.Utils.Lang 183 184**Parameters** 185 186| Name| Type| Mandatory| Description| 187| -------- | -------- | -------- | -------- | 188| key | K | Yes| Target key.| 189 190**Return value** 191 192| Type| Description| 193| -------- | -------- | 194| V | Value obtained.| 195 196**Error codes** 197 198For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 199 200| ID| Error Message| 201| -------- | -------- | 202| 10200011 | The get method cannot be bound. | 203 204**Example** 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 218Adds all elements in a **HashMap** instance to this container. 219 220**Atomic service API**: This API can be used in atomic services since API version 12. 221 222**System capability**: SystemCapability.Utils.Lang 223 224**Parameters** 225 226| Name| Type| Mandatory| Description| 227| -------- | -------- | -------- | -------- | 228| map | HashMap<K, V> | Yes| **HashMap** instance whose elements are to be added to the current container.| 229 230**Error codes** 231 232For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 233 234| ID| Error Message| 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**Example** 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 255Adds or updates an element in this container. 256 257**Atomic service API**: This API can be used in atomic services since API version 12. 258 259**System capability**: SystemCapability.Utils.Lang 260 261**Parameters** 262 263| Name| Type| Mandatory| Description| 264| -------- | -------- | -------- | -------- | 265| key | K | Yes| Key of the target element.| 266| value | V | Yes| Value of the target element.| 267 268**Return value** 269 270| Type| Description| 271| -------- | -------- | 272| Object | Container that contains the new element.| 273 274**Error codes** 275 276For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 277 278| ID| Error Message| 279| -------- | -------- | 280| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. | 281| 10200011 | The set method cannot be bound. | 282 283**Example** 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 295Removes an element with the specified key from this container. 296 297**Atomic service API**: This API can be used in atomic services since API version 12. 298 299**System capability**: SystemCapability.Utils.Lang 300 301**Parameters** 302 303| Name| Type| Mandatory| Description| 304| -------- | -------- | -------- | -------- | 305| key | K | Yes| Key of the target element.| 306 307**Return value** 308 309| Type| Description| 310| -------- | -------- | 311| V | Value of the element.| 312 313**Error codes** 314 315For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 316 317| ID| Error Message| 318| -------- | -------- | 319| 10200011 | The remove method cannot be bound. | 320 321**Example** 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 335Clears this container and sets its length to **0**. 336 337**Atomic service API**: This API can be used in atomic services since API version 12. 338 339**System capability**: SystemCapability.Utils.Lang 340 341**Error codes** 342 343For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 344 345| ID| Error Message| 346| -------- | -------- | 347| 10200011 | The clear method cannot be bound. | 348 349**Example** 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 363Obtains an iterator that contains all the keys in this container. 364 365**Atomic service API**: This API can be used in atomic services since API version 12. 366 367**System capability**: SystemCapability.Utils.Lang 368 369**Return value** 370 371| Type| Description| 372| -------- | -------- | 373| IterableIterator<K> | Iterator obtained.| 374 375**Error codes** 376 377For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 378 379| ID| Error Message| 380| -------- | -------- | 381| 10200011 | The keys method cannot be bound. | 382 383**Example** 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 402Obtains an iterator that contains all the values in this container. 403 404**Atomic service API**: This API can be used in atomic services since API version 12. 405 406**System capability**: SystemCapability.Utils.Lang 407 408**Return value** 409 410| Type| Description| 411| -------- | -------- | 412| IterableIterator<V> | Iterator obtained.| 413 414**Error codes** 415 416For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 417 418| ID| Error Message| 419| -------- | -------- | 420| 10200011 | The values method cannot be bound. | 421 422**Example** 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 441Replaces an element in this container. 442 443**Atomic service API**: This API can be used in atomic services since API version 12. 444 445**System capability**: SystemCapability.Utils.Lang 446 447**Parameters** 448 449| Name| Type| Mandatory| Description| 450| -------- | -------- | -------- | -------- | 451| key | K | Yes| Key of the target element.| 452| newValue | V | Yes| New value of the element.| 453 454**Return value** 455 456| Type| Description| 457| -------- | -------- | 458| boolean | Returns **true** if the element is replaced; returns **false** otherwise.| 459 460**Error codes** 461 462For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 463 464| ID| Error Message| 465| -------- | -------- | 466| 10200011 | The replace method cannot be bound. | 467 468**Example** 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 481Uses a callback to traverse the elements in this container and obtain their position indexes. 482 483**Atomic service API**: This API can be used in atomic services since API version 12. 484 485**System capability**: SystemCapability.Utils.Lang 486 487**Parameters** 488 489| Name| Type| Mandatory| Description| 490| -------- | -------- | -------- | -------- | 491| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 492| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 493 494callbackFn 495| Name| Type| Mandatory| Description| 496| -------- | -------- | -------- | -------- | 497| value | V | No| Value of the element that is currently traversed. The default value is the value of the first key-value pair.| 498| key | K | No| Key of the element that is currently traversed. The default value is the key of the first key-value pair.| 499| map | HashMap<K, V> | No| Instance that calls the **forEach** API. The default value is this instance.| 500 501**Error codes** 502 503For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 504 505| ID| Error Message| 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**Example** 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// You are not advised to use the set or remove APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 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 536Obtains an iterator that contains all the elements in this container. 537 538**Atomic service API**: This API can be used in atomic services since API version 12. 539 540**System capability**: SystemCapability.Utils.Lang 541 542**Return value** 543 544| Type| Description| 545| -------- | -------- | 546| IterableIterator<[K, V]> | Iterator obtained.| 547 548**Error codes** 549 550For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 551 552| ID| Error Message| 553| -------- | -------- | 554| 10200011 | The entries method cannot be bound. | 555 556**Example** 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// You are not advised to use the set or remove APIs in entries because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 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 585Obtains an iterator, each item of which is a JavaScript object. 586 587**Atomic service API**: This API can be used in atomic services since API version 12. 588 589**System capability**: SystemCapability.Utils.Lang 590 591**Return value** 592 593| Type| Description| 594| -------- | -------- | 595| IterableIterator<[K, V]> | Iterator obtained.| 596 597**Error codes** 598 599For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 600 601| ID| Error Message| 602| -------- | -------- | 603| 10200011 | The Symbol.iterator method cannot be bound. | 604 605**Example** 606```ts 607let hashMap: HashMap<string, number> = new HashMap(); 608hashMap.set("squirrel", 123); 609hashMap.set("sparrow", 356); 610 611// Method 1: 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// Method 2: 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// You are not advised to use the set or remove APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data. 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``` 639