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