js-apis-lightweightmap.md 13.6 KB
Newer Older
L
linhaoran 已提交
1 2 3 4 5 6 7 8
# 非线性容器LightWeightMap  

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

9
```ts
10
import LightWeightMap from '@ohos.util.LightWeightMap';  
L
linhaoran 已提交
11 12
```

Z
zengyawen 已提交
13
## 系统能力
L
linhaoran 已提交
14

Z
zengyawen 已提交
15
SystemCapability.Utils.Lang
L
linhaoran 已提交
16 17 18 19 20 21 22 23

## LightWeightMap


### 属性

| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
24
| length | number | 是 | 否 | LightWeightMap的元素个数。 |
L
linhaoran 已提交
25 26 27 28


### constructor

Z
zengyawen 已提交
29
constructor()
L
linhaoran 已提交
30 31 32

LightWeightMap的构造函数。

Z
zengyawen 已提交
33 34
**示例:**

35
```ts
Z
zengyawen 已提交
36 37
let lightWeightMap = new LightWeightMap();
```
L
linhaoran 已提交
38 39 40 41


### isEmpty

Z
zengyawen 已提交
42
isEmpty(): boolean
L
linhaoran 已提交
43 44 45

判断该LightWeightMap是否为空。

Z
zengyawen 已提交
46
**返回值:**
L
linhaoran 已提交
47

Z
zengyawen 已提交
48 49 50 51 52 53
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |

**示例:**

54
```ts
Z
zengyawen 已提交
55
const lightWeightMap = new LightWeightMap();
56
let result = lightWeightMap.isEmpty();
Z
zengyawen 已提交
57
```
L
linhaoran 已提交
58 59 60 61


### hasAll

Z
zengyawen 已提交
62
hasAll(map: LightWeightMap<K, V>): boolean
L
linhaoran 已提交
63 64 65

判断此LightWeightMap中是否含有该指定map中的所有元素。

Z
zengyawen 已提交
66 67 68 69 70 71 72
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| map | LightWeightMap<K, V> | 是 | 比较对象。 |

**返回值:**
L
linhaoran 已提交
73

Z
zengyawen 已提交
74 75 76
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含所有元素返回true,否则返回false。 |
L
linhaoran 已提交
77

Z
zengyawen 已提交
78 79
**示例:**

80
```ts
Z
zengyawen 已提交
81 82 83 84 85 86 87
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let map = new LightWeightMap();
map.set("sdfs", 356);
let result = lightWeightMap.hasAll(map);
```
L
linhaoran 已提交
88 89 90 91 92 93 94 95


### hasKey

hasKey(key: K): boolean;

判断此LightWeightMap中是否含有该指定key。

Z
zengyawen 已提交
96
**参数:**
L
linhaoran 已提交
97

Z
zengyawen 已提交
98 99
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
100
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
101

Z
zengyawen 已提交
102 103 104 105
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
106
| boolean | 包含指定key返回true,否则返回false。 |
Z
zengyawen 已提交
107 108 109

**示例:**

110
```ts
Z
zengyawen 已提交
111
let lightWeightMap = new LightWeightMap();
112
let result = lightWeightMap.hasKey;
Z
zengyawen 已提交
113 114
lightWeightMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
115
let result1 = lightWeightMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
Z
zengyawen 已提交
116
```
L
linhaoran 已提交
117 118 119 120


### hasValue

Z
zengyawen 已提交
121
hasValue(value: V): boolean
L
linhaoran 已提交
122 123 124

判断此LightWeightMap中是否含有该指定value。

Z
zengyawen 已提交
125 126 127 128 129
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | V | 是 | 指定元素。 |
L
linhaoran 已提交
130

Z
zengyawen 已提交
131
**返回值:**
L
linhaoran 已提交
132

Z
zengyawen 已提交
133 134 135 136 137 138
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |

**示例:**

139
```ts
Z
zengyawen 已提交
140
let lightWeightMap = new LightWeightMap();
141
let result = lightWeightMap.hasValue(123);
Z
zengyawen 已提交
142
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
143
let result1 = lightWeightMap.hasValue(123);
Z
zengyawen 已提交
144
```
L
linhaoran 已提交
145 146 147 148


### increaseCapacityTo

Z
zengyawen 已提交
149
increaseCapacityTo(minimumCapacity: number): void
L
linhaoran 已提交
150 151 152

将当前LightWeightMap扩容至可以容纳指定数量元素。

Z
zengyawen 已提交
153
**参数:**
L
linhaoran 已提交
154

Z
zengyawen 已提交
155 156 157
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | 是 | 需要容纳的数量。 |
L
linhaoran 已提交
158

Z
zengyawen 已提交
159 160
**示例:**

161
```ts
Z
zengyawen 已提交
162 163 164
let lightWeightMap = new LightWeightMap();
lightWeightMap.increaseCapacityTo(10);
```
L
linhaoran 已提交
165 166 167 168


### get

Z
zengyawen 已提交
169
get(key: K): V
L
linhaoran 已提交
170 171 172

获取指定key所对应的value。

Z
zengyawen 已提交
173
**参数:**
L
linhaoran 已提交
174

Z
zengyawen 已提交
175 176 177
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
178

Z
zengyawen 已提交
179 180 181 182 183 184 185 186
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| V | 返回key映射的value值。 |

**示例:**

187
```ts
Z
zengyawen 已提交
188 189 190
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
191
let result = lightWeightMap.get("sdfs");
Z
zengyawen 已提交
192
```
L
linhaoran 已提交
193 194 195 196


### getIndexOfKey

Z
zengyawen 已提交
197 198 199
getIndexOfKey(key: K): number

查找指定元素第一次出现的下标值,如果没有找到该元素返回-1。
L
linhaoran 已提交
200

Z
zengyawen 已提交
201
**参数:**
L
linhaoran 已提交
202

Z
zengyawen 已提交
203 204 205
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 被查找的元素。 |
L
linhaoran 已提交
206

Z
zengyawen 已提交
207
**返回值:**
L
linhaoran 已提交
208

Z
zengyawen 已提交
209 210 211 212 213 214
| 类型 | 说明 |
| -------- | -------- |
| number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |

**示例:**

215
```ts
Z
zengyawen 已提交
216 217 218
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
219
let result = lightWeightMap.getIndexOfKey("sdfs");
Z
zengyawen 已提交
220
```
L
linhaoran 已提交
221 222 223 224


### getIndexOfValue

Z
zengyawen 已提交
225 226 227 228 229 230 231 232 233
getIndexOfValue(value: V): number

查找指定元素第一次出现的下标值,如果没有找到该元素返回-1。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | V | 是 | 被查找的元素。 |
L
linhaoran 已提交
234

Z
zengyawen 已提交
235
**返回值:**
L
linhaoran 已提交
236

Z
zengyawen 已提交
237 238 239
| 类型 | 说明 |
| -------- | -------- |
| number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |
L
linhaoran 已提交
240

Z
zengyawen 已提交
241
**示例:**
L
linhaoran 已提交
242

243
```ts
Z
zengyawen 已提交
244 245 246
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
247
let result = lightWeightMap.getIndexOfValue(123);
Z
zengyawen 已提交
248
```
L
linhaoran 已提交
249 250 251 252


### getKeyAt

Z
zengyawen 已提交
253
getKeyAt(index: number): K
L
linhaoran 已提交
254 255 256

查找指定下标的元素键值对中key值,否则返回undefined。

Z
zengyawen 已提交
257 258 259 260 261 262 263
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 所查找的下标。 |

**返回值:**
L
linhaoran 已提交
264

Z
zengyawen 已提交
265 266 267
| 类型 | 说明 |
| -------- | -------- |
| K | 返回该下标对应的元素键值对中key值。 |
L
linhaoran 已提交
268

Z
zengyawen 已提交
269 270
**示例:**

271
```ts
Z
zengyawen 已提交
272 273 274
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
275
let result = lightWeightMap.getKeyAt(1);
Z
zengyawen 已提交
276
```
L
linhaoran 已提交
277 278 279 280


### setAll

Z
zengyawen 已提交
281
setAll(map: LightWeightMap<K, V>): void
L
linhaoran 已提交
282

283
将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。
L
linhaoran 已提交
284

Z
zengyawen 已提交
285
**参数:**
L
linhaoran 已提交
286

Z
zengyawen 已提交
287 288
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
289
| map | LightWeightMap<K, V> | 是 | 被添加元素的lightWeightMap。 |
Z
zengyawen 已提交
290 291 292

**示例:**

293
```ts
Z
zengyawen 已提交
294 295 296 297 298 299
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let map = new LightWeightMap();
lightWeightMap.setAll(map);
```
L
linhaoran 已提交
300 301 302


### set
Z
zengyawen 已提交
303
set(key: K, value: V): Object
L
linhaoran 已提交
304 305 306

向LightWeightMap中添加一组数据。

Z
zengyawen 已提交
307 308 309 310 311 312
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 添加成员数据的键名。 |
| value | V | 是 | 添加成员数据的值。 |
L
linhaoran 已提交
313

Z
zengyawen 已提交
314
**返回值:**
L
linhaoran 已提交
315

Z
zengyawen 已提交
316 317
| 类型 | 说明 |
| -------- | -------- |
318
| Object | 返回添加数据后的lightWeightMap。 |
Z
zengyawen 已提交
319 320 321

**示例:**

322
```ts
Z
zengyawen 已提交
323
let lightWeightMap = new LightWeightMap();
324
let result = lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
Z
zengyawen 已提交
325
```
L
linhaoran 已提交
326 327 328 329


### remove

Z
zengyawen 已提交
330
remove(key: K): V
L
linhaoran 已提交
331

332
删除并返回指定key映射的元素。
L
linhaoran 已提交
333

Z
zengyawen 已提交
334 335 336 337
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
338
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
339

Z
zengyawen 已提交
340
**返回值:**
L
linhaoran 已提交
341

Z
zengyawen 已提交
342 343 344 345 346 347
| 类型 | 说明 |
| -------- | -------- |
| V | 返回删除元素的值。 |

**示例:**

348
```ts
Z
zengyawen 已提交
349 350 351 352 353
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.remove("sdfs");
```
L
linhaoran 已提交
354 355 356 357


### removeAt

Z
zengyawen 已提交
358
removeAt(index: number): boolean
L
linhaoran 已提交
359

360
删除指定下标对应的元素。
L
linhaoran 已提交
361

Z
zengyawen 已提交
362 363 364 365
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
366
| index | number | 是 | 指定下标。 |
Z
zengyawen 已提交
367 368

**返回值:**
L
linhaoran 已提交
369

Z
zengyawen 已提交
370 371 372
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功删除元素返回true,否则返回false。 |
L
linhaoran 已提交
373

Z
zengyawen 已提交
374 375
**示例:**

376
```ts
Z
zengyawen 已提交
377 378 379
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
380
let result = lightWeightMap.removeAt(1);
Z
zengyawen 已提交
381
```
L
linhaoran 已提交
382 383 384 385


### setValueAt

Z
zengyawen 已提交
386
setValueAt(index: number, newValue: V): boolean
L
linhaoran 已提交
387

388
替换指定下标对应键值对中的元素。
L
linhaoran 已提交
389

Z
zengyawen 已提交
390
**参数:**
L
linhaoran 已提交
391

Z
zengyawen 已提交
392 393
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
394
| index | number | 是 | 指定下标。 |
Z
zengyawen 已提交
395
| newValue | V | 是 | 替换键值对中的值。 |
L
linhaoran 已提交
396

Z
zengyawen 已提交
397 398 399 400 401 402 403 404
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功替换指定位置数据返回true,否则返回false。 |

**示例:**

405
```ts
Z
zengyawen 已提交
406 407 408 409 410
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.setValueAt(1, 3546);
```
L
linhaoran 已提交
411 412 413 414


### getValueAt

Z
zengyawen 已提交
415
getValueAt(index: number): V
L
linhaoran 已提交
416

417
获取指定下标对应键值对中的元素。
L
linhaoran 已提交
418

Z
zengyawen 已提交
419 420 421 422
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
423
| index | number | 是 | 指定下标。 |
L
linhaoran 已提交
424

Z
zengyawen 已提交
425
**返回值:**
L
linhaoran 已提交
426

Z
zengyawen 已提交
427 428
| 类型 | 说明 |
| -------- | -------- |
429
| V | 返回指定下标对应键值对中的元素。 |
Z
zengyawen 已提交
430 431 432

**示例:**

433
```ts
Z
zengyawen 已提交
434 435 436
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
437
let result = lightWeightMap.getValueAt(1);
Z
zengyawen 已提交
438
```
L
linhaoran 已提交
439 440 441 442


### clear

Z
zengyawen 已提交
443
clear(): void
L
linhaoran 已提交
444 445 446

清除LightWeightMap中的所有元素,并把length置为0。

Z
zengyawen 已提交
447 448
**示例:**

449
```ts
Z
zengyawen 已提交
450 451 452 453 454
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.clear();
```
L
linhaoran 已提交
455 456 457 458


### keys

Z
zengyawen 已提交
459
keys(): IterableIterator&lt;K&gt;
L
linhaoran 已提交
460 461 462

返回包含此映射中包含的键的新迭代器对象。

Z
zengyawen 已提交
463 464 465 466 467 468 469 470
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;K&gt; | 返回一个迭代器。 |

**示例:**

471
```ts
Z
zengyawen 已提交
472 473 474 475 476 477
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
478
  console.log("value:" + temp);
Z
zengyawen 已提交
479 480 481
  temp = iter.next().value;
}
```
L
linhaoran 已提交
482 483 484 485


### values

Z
zengyawen 已提交
486
values(): IterableIterator&lt;V&gt;
L
linhaoran 已提交
487

W
wusongqing 已提交
488
返回包含此映射中包含的键值的新迭代器对象。
L
linhaoran 已提交
489

Z
zengyawen 已提交
490 491 492 493 494 495 496 497
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;V&gt; | 返回一个迭代器。 |

**示例:**

498
```ts
Z
zengyawen 已提交
499 500 501 502 503 504
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.values();
let temp = iter.next().value;
while(temp != undefined) {
505
  console.log("value:" + temp);
Z
zengyawen 已提交
506 507 508
  temp = iter.next().value;
} 
```
L
linhaoran 已提交
509 510 511 512


### forEach

513
forEach(callbackfn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
L
linhaoran 已提交
514 515 516

通过回调函数来遍历实例对象上的元素以及元素对应的下标。

Z
zengyawen 已提交
517 518 519 520 521 522 523 524 525 526
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackfn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |

callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
527 528
| value | V | 否 | 当前遍历到的元素键值对的值。 |
| key | K | 否 | 当前遍历到的元素键值对的键。 |
529
| map | LightWeightMap<K, V> | 否 | 当前调用forEach方法的实例对象。 |
Z
zengyawen 已提交
530 531 532

**示例:**

533
```ts
Z
zengyawen 已提交
534 535 536 537
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("sdfs", 123);
lightWeightMap.set("dfsghsf", 357);
lightWeightMap.forEach((value, key) => {
538
  console.log("value:" + value, key);
Z
zengyawen 已提交
539 540
});
```
L
linhaoran 已提交
541 542 543 544


### entries

Z
zengyawen 已提交
545
entries(): IterableIterator<[K, V]>
L
linhaoran 已提交
546

547
返回包含此映射中包含的键值对的新迭代器对象。
L
linhaoran 已提交
548

Z
zengyawen 已提交
549 550 551 552 553 554 555 556
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator<[K, V]> | 返回一个迭代器。 |

**示例:**

557
```ts
Z
zengyawen 已提交
558 559 560 561 562 563
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
564 565
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
566 567 568
  temp = iter.next().value;
}
```
L
linhaoran 已提交
569

570 571
### toString

572
toString(): String
573 574 575 576 577 578 579

将此映射中包含的键值对拼接成字符串,并返回字符串类型。

**返回值:**

  | 类型 | 说明 |
  | -------- | -------- |
580
  | String | 返回一个字符串。 |
581 582 583

**示例:**

584
  ```ts
585 586 587 588 589
  let lightWeightMap = new LightWeightMap();
  lightWeightMap.set("A", 123);
  lightWeightMap.set("sdfs", 356);
  let iter = lightWeightMap.toString();
  ```
L
linhaoran 已提交
590 591 592

### [Symbol.iterator]

Z
zengyawen 已提交
593
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
L
linhaoran 已提交
594 595 596

返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。

Z
zengyawen 已提交
597 598 599 600 601 602 603 604
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator<[K, V]> | 返回一个迭代器。 |

**示例:**

605
```ts
Z
zengyawen 已提交
606 607 608 609 610 611
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);

// 使用方法一:
for (let item of lightWeightMap) { 
612 613
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
Z
zengyawen 已提交
614 615 616 617 618 619
}

// 使用方法二:
let iter = lightWeightMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
620 621
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
622 623 624
  temp = iter.next().value;
}
```