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