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