js-apis-lightweightmap.md 20.6 KB
Newer Older
1
# @ohos.util.LightWeightMap (非线性容器LightWeightMap)  
L
linhaoran 已提交
2

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
L
linhaoran 已提交
4 5
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

6 7
LightWeightMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。

B
bi-hu 已提交
8 9 10
LightWeightMap依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。

集合中key值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的key值及value值。
11 12 13 14

LightWeightMap和[HashMap](js-apis-hashmap.md)都是用来存储键值对的集合,LightWeightMap占用内存更小。

**推荐使用场景:** 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。
L
linhaoran 已提交
15

L
lengchangjing 已提交
16 17 18 19
文档中存在泛型的使用,涉及以下泛型标记符:<br>
- K: Key, 键<br>
- V: Value, 值

L
linhaoran 已提交
20 21
## 导入模块

22
```ts
23
import LightWeightMap from '@ohos.util.LightWeightMap';  
L
linhaoran 已提交
24 25 26 27 28 29
```

## LightWeightMap

### 属性

Z
zengyawen 已提交
30 31
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
32
| 名称 | 类型 | 可读 | 可写 | 说明 |
L
linhaoran 已提交
33
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
34
| length | number | 是 | 否 | LightWeightMap的元素个数。 |
L
linhaoran 已提交
35 36 37 38


### constructor

Z
zengyawen 已提交
39
constructor()
L
linhaoran 已提交
40 41 42

LightWeightMap的构造函数。

Z
zengyawen 已提交
43 44
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
45 46 47

**错误码:**

B
bi-hu 已提交
48
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
49

L
liu-ganlin 已提交
50
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
51 52 53
| -------- | -------- |
| 10200012 | The LightWeightMap's constructor cannot be directly invoked. |

Z
zengyawen 已提交
54 55
**示例:**

56
```ts
Z
zengyawen 已提交
57 58
let lightWeightMap = new LightWeightMap();
```
L
linhaoran 已提交
59 60 61 62


### isEmpty

Z
zengyawen 已提交
63
isEmpty(): boolean
L
linhaoran 已提交
64 65 66

判断该LightWeightMap是否为空。

Z
zengyawen 已提交
67 68
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
69
**返回值:**
L
linhaoran 已提交
70

Z
zengyawen 已提交
71 72 73 74
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |

L
liu-ganlin 已提交
75 76
**错误码:**

B
bi-hu 已提交
77
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
78

L
liu-ganlin 已提交
79
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
80 81 82
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

Z
zengyawen 已提交
83 84
**示例:**

85
```ts
Z
zengyawen 已提交
86
const lightWeightMap = new LightWeightMap();
87
let result = lightWeightMap.isEmpty();
Z
zengyawen 已提交
88
```
L
linhaoran 已提交
89 90 91 92


### hasAll

Z
zengyawen 已提交
93
hasAll(map: LightWeightMap<K, V>): boolean
L
linhaoran 已提交
94 95 96

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

Z
zengyawen 已提交
97 98
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
99 100 101 102 103 104 105
**参数:**

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

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

Z
zengyawen 已提交
107 108 109
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含所有元素返回true,否则返回false。 |
L
linhaoran 已提交
110

L
liu-ganlin 已提交
111 112
**错误码:**

B
bi-hu 已提交
113
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
114

L
liu-ganlin 已提交
115
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
116 117 118
| -------- | -------- |
| 10200011 | The hasAll method cannot be bound. |

Z
zengyawen 已提交
119 120
**示例:**

121
```ts
Z
zengyawen 已提交
122
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
123 124
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
125
let map = new LightWeightMap();
L
lengchangjing 已提交
126
map.set("sparrow", 356);
Z
zengyawen 已提交
127 128
let result = lightWeightMap.hasAll(map);
```
L
linhaoran 已提交
129 130 131 132 133 134 135 136


### hasKey

hasKey(key: K): boolean;

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

Z
zengyawen 已提交
137 138
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
139
**参数:**
L
linhaoran 已提交
140

Z
zengyawen 已提交
141 142
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
143
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
144

Z
zengyawen 已提交
145 146 147 148
**返回值:**

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

L
liu-ganlin 已提交
151 152
**错误码:**

B
bi-hu 已提交
153
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
154

L
liu-ganlin 已提交
155
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
156 157 158
| -------- | -------- |
| 10200011 | The hasKey method cannot be bound. |

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

161
```ts
Z
zengyawen 已提交
162
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
163
lightWeightMap.set("squirrel", 123);
B
bi-hu 已提交
164
let result = lightWeightMap.hasKey("squirrel");
Z
zengyawen 已提交
165
```
L
linhaoran 已提交
166 167 168 169


### hasValue

Z
zengyawen 已提交
170
hasValue(value: V): boolean
L
linhaoran 已提交
171 172 173

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

Z
zengyawen 已提交
174 175
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
176 177 178 179 180
**参数:**

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

Z
zengyawen 已提交
182
**返回值:**
L
linhaoran 已提交
183

Z
zengyawen 已提交
184 185 186 187
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |

L
liu-ganlin 已提交
188 189
**错误码:**

B
bi-hu 已提交
190
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
191

L
liu-ganlin 已提交
192
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
193 194 195
| -------- | -------- |
| 10200011 | The hasValue method cannot be bound. |

Z
zengyawen 已提交
196 197
**示例:**

198
```ts
Z
zengyawen 已提交
199
let lightWeightMap = new LightWeightMap();
200
let result = lightWeightMap.hasValue(123);
L
lengchangjing 已提交
201
lightWeightMap.set("squirrel", 123);
202
let result1 = lightWeightMap.hasValue(123);
Z
zengyawen 已提交
203
```
L
linhaoran 已提交
204 205 206 207


### increaseCapacityTo

Z
zengyawen 已提交
208
increaseCapacityTo(minimumCapacity: number): void
L
linhaoran 已提交
209 210 211

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

Z
zengyawen 已提交
212 213
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
214 215
**错误码:**

B
bi-hu 已提交
216
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
217

L
liu-ganlin 已提交
218
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
219 220 221
| -------- | -------- |
| 10200011 | The increaseCapacityTo method cannot be bound. |

Z
zengyawen 已提交
222
**参数:**
L
linhaoran 已提交
223

Z
zengyawen 已提交
224 225 226
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | 是 | 需要容纳的数量。 |
L
linhaoran 已提交
227

Z
zengyawen 已提交
228 229
**示例:**

230
```ts
Z
zengyawen 已提交
231 232 233
let lightWeightMap = new LightWeightMap();
lightWeightMap.increaseCapacityTo(10);
```
L
linhaoran 已提交
234 235 236 237


### get

Z
zengyawen 已提交
238
get(key: K): V
L
linhaoran 已提交
239 240 241

获取指定key所对应的value。

Z
zengyawen 已提交
242 243
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
244
**参数:**
L
linhaoran 已提交
245

Z
zengyawen 已提交
246 247 248
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 指定key。 |
L
linhaoran 已提交
249

Z
zengyawen 已提交
250 251 252 253 254 255
**返回值:**

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

L
liu-ganlin 已提交
256 257
**错误码:**

B
bi-hu 已提交
258
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
259

L
liu-ganlin 已提交
260
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
261 262 263
| -------- | -------- |
| 10200011 | The get method cannot be bound. |

Z
zengyawen 已提交
264 265
**示例:**

266
```ts
Z
zengyawen 已提交
267
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
268 269 270
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.get("sparrow");
Z
zengyawen 已提交
271
```
L
linhaoran 已提交
272 273 274 275


### getIndexOfKey

Z
zengyawen 已提交
276 277
getIndexOfKey(key: K): number

B
bi-hu 已提交
278
查找key元素第一次出现的下标值,如果没有找到该元素返回-1。
L
linhaoran 已提交
279

Z
zengyawen 已提交
280 281
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
282
**参数:**
L
linhaoran 已提交
283

Z
zengyawen 已提交
284 285 286
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | K | 是 | 被查找的元素。 |
L
linhaoran 已提交
287

Z
zengyawen 已提交
288
**返回值:**
L
linhaoran 已提交
289

Z
zengyawen 已提交
290 291
| 类型 | 说明 |
| -------- | -------- |
B
bi-hu 已提交
292
| number | 返回key元素第一次出现时的下标值,查找失败返回-1。 |
Z
zengyawen 已提交
293

L
liu-ganlin 已提交
294 295
**错误码:**

B
bi-hu 已提交
296
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
297

L
liu-ganlin 已提交
298
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
299 300 301
| -------- | -------- |
| 10200011 | The getIndexOfKey method cannot be bound. |

Z
zengyawen 已提交
302 303
**示例:**

304
```ts
Z
zengyawen 已提交
305
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
306 307 308
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfKey("sparrow");
Z
zengyawen 已提交
309
```
L
linhaoran 已提交
310 311 312 313


### getIndexOfValue

Z
zengyawen 已提交
314 315
getIndexOfValue(value: V): number

B
bi-hu 已提交
316
查找value元素第一次出现的下标值,如果没有找到该元素返回-1。
Z
zengyawen 已提交
317

Z
zengyawen 已提交
318 319
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
320 321 322 323 324
**参数:**

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

Z
zengyawen 已提交
326
**返回值:**
L
linhaoran 已提交
327

Z
zengyawen 已提交
328 329
| 类型 | 说明 |
| -------- | -------- |
B
bi-hu 已提交
330
| number | 返回value元素第一次出现时的下标值,查找失败返回-1。 |
L
linhaoran 已提交
331

L
liu-ganlin 已提交
332 333
**错误码:**

B
bi-hu 已提交
334
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
335

L
liu-ganlin 已提交
336
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
337 338 339
| -------- | -------- |
| 10200011 | The getIndexOfValue method cannot be bound. |

Z
zengyawen 已提交
340
**示例:**
L
linhaoran 已提交
341

342
```ts
Z
zengyawen 已提交
343
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
344 345
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
346
let result = lightWeightMap.getIndexOfValue(123);
Z
zengyawen 已提交
347
```
L
linhaoran 已提交
348 349 350 351


### getKeyAt

Z
zengyawen 已提交
352
getKeyAt(index: number): K
L
linhaoran 已提交
353 354 355

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

Z
zengyawen 已提交
356 357
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
358 359 360 361 362 363 364
**参数:**

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

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

Z
zengyawen 已提交
366 367 368
| 类型 | 说明 |
| -------- | -------- |
| K | 返回该下标对应的元素键值对中key值。 |
L
linhaoran 已提交
369

L
liu-ganlin 已提交
370 371
**错误码:**

B
bi-hu 已提交
372
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
373

L
liu-ganlin 已提交
374
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
375 376
| -------- | -------- |
| 10200011 | The getKeyAt method cannot be bound. |
L
liu-ganlin 已提交
377
| 10200001 | The value of index is out of range. |
L
liu-ganlin 已提交
378

Z
zengyawen 已提交
379 380
**示例:**

381
```ts
Z
zengyawen 已提交
382
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
383 384
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
385
let result = lightWeightMap.getKeyAt(1);
Z
zengyawen 已提交
386
```
L
linhaoran 已提交
387 388 389 390


### setAll

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

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

Z
zengyawen 已提交
395 396
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
397
**参数:**
L
linhaoran 已提交
398

Z
zengyawen 已提交
399 400
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
401
| map | LightWeightMap<K, V> | 是 | 被添加元素的lightWeightMap。 |
Z
zengyawen 已提交
402

L
liu-ganlin 已提交
403 404
**错误码:**

B
bi-hu 已提交
405
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
406

L
liu-ganlin 已提交
407
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
408 409 410
| -------- | -------- |
| 10200011 | The setAll method cannot be bound. |

Z
zengyawen 已提交
411 412
**示例:**

413
```ts
Z
zengyawen 已提交
414
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
415 416
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
417
let map = new LightWeightMap();
B
bi-hu 已提交
418
map.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中
Z
zengyawen 已提交
419
```
L
linhaoran 已提交
420 421 422


### set
Z
zengyawen 已提交
423
set(key: K, value: V): Object
L
linhaoran 已提交
424 425 426

向LightWeightMap中添加一组数据。

Z
zengyawen 已提交
427 428
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
429 430 431 432 433 434
**参数:**

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

Z
zengyawen 已提交
436
**返回值:**
L
linhaoran 已提交
437

Z
zengyawen 已提交
438 439
| 类型 | 说明 |
| -------- | -------- |
440
| Object | 返回添加数据后的lightWeightMap。 |
Z
zengyawen 已提交
441

L
liu-ganlin 已提交
442 443
**错误码:**

B
bi-hu 已提交
444
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
445

L
liu-ganlin 已提交
446
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
447 448 449
| -------- | -------- |
| 10200011 | The set method cannot be bound. |

Z
zengyawen 已提交
450 451
**示例:**

452
```ts
Z
zengyawen 已提交
453
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
454
let result = lightWeightMap.set("squirrel", 123);
Z
zengyawen 已提交
455
```
L
linhaoran 已提交
456 457 458 459


### remove

Z
zengyawen 已提交
460
remove(key: K): V
L
linhaoran 已提交
461

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

Z
zengyawen 已提交
464 465
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
466 467 468 469
**参数:**

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

Z
zengyawen 已提交
472
**返回值:**
L
linhaoran 已提交
473

Z
zengyawen 已提交
474 475 476 477
| 类型 | 说明 |
| -------- | -------- |
| V | 返回删除元素的值。 |

L
liu-ganlin 已提交
478 479
**错误码:**

B
bi-hu 已提交
480
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
481

L
liu-ganlin 已提交
482
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
483 484 485
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

Z
zengyawen 已提交
486 487
**示例:**

488
```ts
Z
zengyawen 已提交
489
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
490 491 492
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.remove("sparrow");
Z
zengyawen 已提交
493
```
L
linhaoran 已提交
494 495 496 497


### removeAt

Z
zengyawen 已提交
498
removeAt(index: number): boolean
L
linhaoran 已提交
499

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

Z
zengyawen 已提交
502 503
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
504 505 506 507
**参数:**

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

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

Z
zengyawen 已提交
512 513 514
| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功删除元素返回true,否则返回false。 |
L
linhaoran 已提交
515

L
liu-ganlin 已提交
516 517
**错误码:**

B
bi-hu 已提交
518
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
519

L
liu-ganlin 已提交
520
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
521 522 523
| -------- | -------- |
| 10200011 | The removeAt method cannot be bound. |

Z
zengyawen 已提交
524 525
**示例:**

526
```ts
Z
zengyawen 已提交
527
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
528 529
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
530
let result = lightWeightMap.removeAt(1);
Z
zengyawen 已提交
531
```
L
linhaoran 已提交
532 533 534 535


### setValueAt

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

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

Z
zengyawen 已提交
540 541
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
542
**参数:**
L
linhaoran 已提交
543

Z
zengyawen 已提交
544 545
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
546
| index | number | 是 | 指定下标。 |
Z
zengyawen 已提交
547
| newValue | V | 是 | 替换键值对中的值。 |
L
linhaoran 已提交
548

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

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

L
liu-ganlin 已提交
555 556
**错误码:**

B
bi-hu 已提交
557
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
558

L
liu-ganlin 已提交
559
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
560 561
| -------- | -------- |
| 10200011 | The setValueAt method cannot be bound. |
L
liu-ganlin 已提交
562
| 10200001 | The value of index is out of range. |
L
liu-ganlin 已提交
563

Z
zengyawen 已提交
564 565
**示例:**

566
```ts
Z
zengyawen 已提交
567
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
568 569
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
570 571
lightWeightMap.setValueAt(1, 3546);
```
L
linhaoran 已提交
572 573 574 575


### getValueAt

Z
zengyawen 已提交
576
getValueAt(index: number): V
L
linhaoran 已提交
577

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

Z
zengyawen 已提交
580 581
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
582 583 584 585
**参数:**

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

Z
zengyawen 已提交
588
**返回值:**
L
linhaoran 已提交
589

Z
zengyawen 已提交
590 591
| 类型 | 说明 |
| -------- | -------- |
592
| V | 返回指定下标对应键值对中的元素。 |
Z
zengyawen 已提交
593

L
liu-ganlin 已提交
594 595
**错误码:**

B
bi-hu 已提交
596
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
597

L
liu-ganlin 已提交
598
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
599 600
| -------- | -------- |
| 10200011 | The getValueAt method cannot be bound. |
L
liu-ganlin 已提交
601
| 10200001 | The value of index is out of range. |
L
liu-ganlin 已提交
602

Z
zengyawen 已提交
603 604
**示例:**

605
```ts
Z
zengyawen 已提交
606
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
607 608
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
609
let result = lightWeightMap.getValueAt(1);
Z
zengyawen 已提交
610
```
L
linhaoran 已提交
611 612 613 614


### clear

Z
zengyawen 已提交
615
clear(): void
L
linhaoran 已提交
616 617 618

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

Z
zengyawen 已提交
619 620
**系统能力:** SystemCapability.Utils.Lang

L
liu-ganlin 已提交
621 622
**错误码:**

B
bi-hu 已提交
623
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
624

L
liu-ganlin 已提交
625
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
626 627 628
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

Z
zengyawen 已提交
629 630
**示例:**

631
```ts
Z
zengyawen 已提交
632
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
633 634
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
635 636
lightWeightMap.clear();
```
L
linhaoran 已提交
637 638 639 640


### keys

Z
zengyawen 已提交
641
keys(): IterableIterator&lt;K&gt;
L
linhaoran 已提交
642 643 644

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

Z
zengyawen 已提交
645 646
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
647 648 649 650 651 652
**返回值:**

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

L
liu-ganlin 已提交
653 654
**错误码:**

B
bi-hu 已提交
655
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
656

L
liu-ganlin 已提交
657
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
658 659 660
| -------- | -------- |
| 10200011 | The keys method cannot be bound. |

Z
zengyawen 已提交
661 662
**示例:**

663
```ts
Z
zengyawen 已提交
664
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
665 666
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
667 668 669
let iter = lightWeightMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
670
  console.log("value:" + temp);
Z
zengyawen 已提交
671 672 673
  temp = iter.next().value;
}
```
L
linhaoran 已提交
674 675 676 677


### values

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

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

Z
zengyawen 已提交
682 683
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
684 685 686 687 688 689
**返回值:**

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

L
liu-ganlin 已提交
690 691
**错误码:**

B
bi-hu 已提交
692
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
693

L
liu-ganlin 已提交
694
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
695 696 697
| -------- | -------- |
| 10200011 | The values method cannot be bound. |

Z
zengyawen 已提交
698 699
**示例:**

700
```ts
Z
zengyawen 已提交
701
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
702 703
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
704 705 706
let iter = lightWeightMap.values();
let temp = iter.next().value;
while(temp != undefined) {
707
  console.log("value:" + temp);
Z
zengyawen 已提交
708
  temp = iter.next().value;
L
liu-ganlin 已提交
709
}
Z
zengyawen 已提交
710
```
L
linhaoran 已提交
711 712 713 714


### forEach

715
forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
L
linhaoran 已提交
716 717 718

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

Z
zengyawen 已提交
719 720
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
721 722 723 724
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
725
| callbackFn | function | 是 | 回调函数。 |
Z
zengyawen 已提交
726 727 728 729 730
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |

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

L
liu-ganlin 已提交
735 736
**错误码:**

B
bi-hu 已提交
737
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
738

L
liu-ganlin 已提交
739
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
740 741 742
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

Z
zengyawen 已提交
743 744
**示例:**

745
```ts
Z
zengyawen 已提交
746
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
747 748
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("gull", 357);
Z
zengyawen 已提交
749
lightWeightMap.forEach((value, key) => {
750
    console.log("value:" + value, "key:" + key);
Z
zengyawen 已提交
751 752
});
```
L
linhaoran 已提交
753 754 755 756


### entries

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

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

Z
zengyawen 已提交
761 762
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
763 764 765 766 767 768
**返回值:**

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

L
liu-ganlin 已提交
769 770
**错误码:**

B
bi-hu 已提交
771
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
772

L
liu-ganlin 已提交
773
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
774 775 776
| -------- | -------- |
| 10200011 | The entries method cannot be bound. |

Z
zengyawen 已提交
777 778
**示例:**

779
```ts
Z
zengyawen 已提交
780
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
781 782
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
783 784 785
let iter = lightWeightMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
786 787
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
Z
zengyawen 已提交
788 789 790
  temp = iter.next().value;
}
```
L
linhaoran 已提交
791

792 793
### toString

794
toString(): String
795 796 797

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

Z
zengyawen 已提交
798 799
**系统能力:** SystemCapability.Utils.Lang

800 801 802 803
**返回值:**

  | 类型 | 说明 |
  | -------- | -------- |
804
  | String | 返回一个字符串。 |
805

L
liu-ganlin 已提交
806 807
**错误码:**

B
bi-hu 已提交
808
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
809

L
liu-ganlin 已提交
810
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
811 812 813
| -------- | -------- |
| 10200011 | The toString method cannot be bound. |

814 815
**示例:**

L
liu-ganlin 已提交
816 817 818 819
```ts
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
B
bi-hu 已提交
820
let result = lightWeightMap.toString();
L
liu-ganlin 已提交
821
```
L
linhaoran 已提交
822 823 824

### [Symbol.iterator]

Z
zengyawen 已提交
825
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
L
linhaoran 已提交
826 827 828

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

Z
zengyawen 已提交
829 830
**系统能力:** SystemCapability.Utils.Lang

Z
zengyawen 已提交
831 832 833 834 835 836
**返回值:**

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

L
liu-ganlin 已提交
837 838
**错误码:**

B
bi-hu 已提交
839
以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)
L
liu-ganlin 已提交
840

L
liu-ganlin 已提交
841
| 错误码ID | 错误信息 |
L
liu-ganlin 已提交
842 843 844
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

Z
zengyawen 已提交
845 846
**示例:**

847
```ts
Z
zengyawen 已提交
848
let lightWeightMap = new LightWeightMap();
L
lengchangjing 已提交
849 850
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
Z
zengyawen 已提交
851 852 853

// 使用方法一:
for (let item of lightWeightMap) { 
854 855
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
Z
zengyawen 已提交
856 857 858 859 860 861
}

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