• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Nonlinear Container HashMap
2
3> **NOTE**
4>
5> 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.
6
7**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.
8
9**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.
10
11**[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.
12
13**Recommended use case**: Use **HashMap** when you need to quickly access, remove, and insert key-value pairs.
14
15## Modules to Import
16
17```ts
18import HashMap from '@ohos.util.HashMap';
19```
20
21## HashMap
22
23### Attributes
24
25**System capability**: SystemCapability.Utils.Lang
26
27| Name| Type| Readable| Writable| Description|
28| -------- | -------- | -------- | -------- | -------- |
29| length | number | Yes| No| Number of elements in a hash map (called container later).|
30
31
32### constructor
33
34constructor()
35
36A constructor used to create a **HashMap** instance.
37
38**System capability**: SystemCapability.Utils.Lang
39
40**Example**
41
42```ts
43let hashMap = new HashMap();
44```
45
46
47### isEmpty
48
49isEmpty(): boolean
50
51Checks whether this container is empty (contains no element).
52
53**System capability**: SystemCapability.Utils.Lang
54
55**Return value**
56
57| Type| Description|
58| -------- | -------- |
59| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
60
61**Example**
62
63```ts
64const hashMap = new HashMap();
65let result = hashMap.isEmpty();
66```
67
68
69### hasKey
70
71hasKey(key: K): boolean
72
73Checks whether this container contains the specified key.
74
75**System capability**: SystemCapability.Utils.Lang
76
77**Parameters**
78
79| Name| Type| Mandatory| Description|
80| -------- | -------- | -------- | -------- |
81| key | K | Yes| Target key.|
82
83**Return value**
84
85| Type| Description|
86| -------- | -------- |
87| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
88
89**Example**
90
91```ts
92let hashMap = new HashMap();
93let result = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
94hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
95let result1 = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
96```
97
98
99### hasValue
100
101hasValue(value: V): boolean
102
103Checks whether this container contains the specified value.
104
105**System capability**: SystemCapability.Utils.Lang
106
107**Parameters**
108
109| Name| Type| Mandatory| Description|
110| -------- | -------- | -------- | -------- |
111| value | V | Yes| Target value.|
112
113**Return value**
114
115| Type| Description|
116| -------- | -------- |
117| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
118
119**Example**
120
121```ts
122let hashMap = new HashMap();
123let result = hashMap.hasValue(123);
124hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
125let result1 = hashMap.hasValue(123);
126```
127
128
129### get
130
131get(key: K): V
132
133Obtains the value of the specified key in this container.
134
135**System capability**: SystemCapability.Utils.Lang
136
137**Parameters**
138
139| Name| Type| Mandatory| Description|
140| -------- | -------- | -------- | -------- |
141| key | K | Yes| Target key.|
142
143**Return value**
144
145| Type| Description|
146| -------- | -------- |
147| V | Value obtained.|
148
149**Example**
150
151```ts
152let hashMap = new HashMap();
153hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
154hashMap.set("sdfs", 356);
155let result = hashMap.get("sdfs");
156```
157
158
159### setAll
160
161setAll(map: HashMap<K, V>): void
162
163Adds all elements in a **HashMap** instance to this container.
164
165**System capability**: SystemCapability.Utils.Lang
166
167**Parameters**
168
169| Name| Type| Mandatory| Description|
170| -------- | -------- | -------- | -------- |
171| map | HashMap<K, V> | Yes| **HashMap** instance whose elements are to be added to the current container.|
172
173**Example**
174
175```ts
176let hashMap = new HashMap();
177hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
178hashMap.set("sdfs", 356);
179let newHashMap = new HashMap();
180hashMap.setAll(newHashMap);
181```
182
183
184### set
185
186set(key: K, value: V): Object
187
188Adds an element to this container.
189
190**System capability**: SystemCapability.Utils.Lang
191
192**Parameters**
193
194| Name| Type| Mandatory| Description|
195| -------- | -------- | -------- | -------- |
196| key | K | Yes| Key of the target element.|
197| value | V | Yes| Value of the element.|
198
199**Return value**
200
201| Type| Description|
202| -------- | -------- |
203| Object | Container that contains the new element.|
204
205**Example**
206
207```ts
208let hashMap = new HashMap();
209let result = hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
210```
211
212
213### remove
214
215remove(key: K): V
216
217Removes an element with the specified key from this container.
218
219**System capability**: SystemCapability.Utils.Lang
220
221**Parameters**
222
223| Name| Type| Mandatory| Description|
224| -------- | -------- | -------- | -------- |
225| key | K | Yes| Key of the target element.|
226
227**Return value**
228
229| Type| Description|
230| -------- | -------- |
231| V | Value of the element.|
232
233**Example**
234
235```ts
236let hashMap = new HashMap();
237hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
238hashMap.set("sdfs", 356);
239let result = hashMap.remove("sdfs");
240```
241
242
243### clear
244
245clear(): void
246
247Clears this container and sets its length to **0**.
248
249**System capability**: SystemCapability.Utils.Lang
250
251**Example**
252
253```ts
254let hashMap = new HashMap();
255hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
256hashMap.set("sdfs", 356);
257hashMap.clear();
258```
259
260
261### keys
262
263keys(): IterableIterator&lt;K&gt;
264
265Obtains an iterator that contains all the elements in this container.
266
267**System capability**: SystemCapability.Utils.Lang
268
269**Return value**
270
271| Type| Description|
272| -------- | -------- |
273| IterableIterator&lt;K&gt; | Iterator obtained.|
274
275**Example**
276
277```ts
278let hashMap = new HashMap();
279hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
280hashMap.set("sdfs", 356);
281let iter = hashMap.keys();
282let temp = iter.next().value;
283while(temp != undefined) {
284  console.log("value:" + temp);
285  temp = iter.next().value;
286}
287```
288
289
290### values
291
292values(): IterableIterator&lt;V&gt;
293
294Obtains an iterator that contains all the values in this container.
295
296**System capability**: SystemCapability.Utils.Lang
297
298**Return value**
299
300| Type| Description|
301| -------- | -------- |
302| IterableIterator&lt;V&gt; | Iterator obtained.|
303
304**Example**
305
306```ts
307let hashMap = new HashMap();
308hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
309hashMap.set("sdfs", 356);
310let iter = hashMap.values();
311let temp = iter.next().value;
312while(temp != undefined) {
313  console.log("value:" + temp);
314  temp = iter.next().value;
315}
316```
317
318
319### replace
320
321replace(key: K, newValue: V): boolean
322
323Replaces an element in this container.
324
325**System capability**: SystemCapability.Utils.Lang
326
327**Parameters**
328
329| Name| Type| Mandatory| Description|
330| -------- | -------- | -------- | -------- |
331| key | K | Yes| Key of the target element.|
332| newValue | V | Yes| New value of the element.|
333
334**Return value**
335
336| Type| Description|
337| -------- | -------- |
338| boolean | Returns **true** if the element is replaced successfully; returns **false** otherwise.|
339
340**Example**
341
342```ts
343let hashMap = new HashMap();
344hashMap.set("sdfs", 123);
345let result = hashMap.replace("sdfs", 357);
346```
347
348
349### forEach
350
351forEach(callbackfn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void
352
353Uses a callback to traverse the elements in this container and obtain their position indexes.
354
355**System capability**: SystemCapability.Utils.Lang
356
357**Parameters**
358
359| Name| Type| Mandatory| Description|
360| -------- | -------- | -------- | -------- |
361| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
362| thisArg | Object | No| Value to use when the callback is invoked.|
363
364callbackfn
365| Name| Type| Mandatory| Description|
366| -------- | -------- | -------- | -------- |
367| value | V | No| Value of the element that is currently traversed.|
368| key | K | No| Key of the element that is currently traversed.|
369| map | HashMap<K, V> | No| Instance that invokes the **forEach** method.|
370
371**Example**
372
373```ts
374let hashMap = new HashMap();
375hashMap.set("sdfs", 123);
376hashMap.set("dfsghsf", 357);
377hashMap.forEach((value, key) => {
378    console.log("value:" + value, "key:" + key);
379});
380```
381
382
383### entries
384
385entries(): IterableIterator&lt;[K, V]&gt;
386
387Obtains an iterator that contains all the elements in this container.
388
389**System capability**: SystemCapability.Utils.Lang
390
391**Return value**
392
393| Type| Description|
394| -------- | -------- |
395| IterableIterator&lt;[K, V]&gt; | Iterator obtained.|
396
397**Example**
398
399```ts
400let hashMap = new HashMap();
401hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
402hashMap.set("sdfs", 356);
403let iter = hashMap.entries();
404let temp = iter.next().value;
405while(temp != undefined) {
406  console.log("key:" + temp[0]);
407  console.log("value:" + temp[1]);
408  temp = iter.next().value;
409}
410```
411
412
413### [Symbol.iterator]
414
415[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
416
417Obtains an iterator, each item of which is a JavaScript object.
418
419**System capability**: SystemCapability.Utils.Lang
420
421**Return value**
422
423| Type| Description|
424| -------- | -------- |
425| IterableIterator&lt;[K, V]&gt; | Iterator obtained.|
426
427**Example**
428```ts
429let hashMap = new HashMap();
430hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
431hashMap.set("sdfs", 356);
432
433// Method 1:
434for (let item of hashMap) {
435  console.log("key:" + item[0]);
436  console.log("value:" + item[1]);
437}
438
439// Method 2:
440let iter = hashMap[Symbol.iterator]();
441let temp = iter.next().value;
442while(temp != undefined) {
443  console.log("key:" + temp[0]);
444  console.log("value:" + temp[1]);
445  temp = iter.next().value;
446}
447```
448