LightWeightMap可用于存储具有关联关系的key-value键值对集合,存储元素中key值唯一,每个key对应一个value。
LightWeightMap依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。
集合中key值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的key值及value值。
LightWeightMap和HashMap都是用来存储键值对的集合,LightWeightMap占用内存更小。
推荐使用场景: 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。
文档中存在泛型的使用,涉及以下泛型标记符:
- K:Key,键
- V:Value,值
说明:
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
import LightWeightMap from '@ohos.util.LightWeightMap';
LightWeightMap
属性
系统能力: SystemCapability.Utils.Lang
名称 | 类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
length | number | 是 | 否 | LightWeightMap的元素个数。 |
constructor
constructor()
LightWeightMap的构造函数。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200012 | The LightWeightMap's constructor cannot be directly invoked. |
示例:
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
isEmpty
isEmpty(): boolean
判断该LightWeightMap是否为空。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
boolean | 为空返回true,不为空返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The isEmpty method cannot be bound. |
示例:
const lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
let result = lightWeightMap.isEmpty();
hasAll
hasAll(map: LightWeightMap<K, V>): boolean
判断此LightWeightMap中是否含有该指定map中的所有元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
map | LightWeightMap<K, V> | 是 | 比较对象。 |
返回值:
类型 | 说明 |
---|---|
boolean | 包含所有元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The hasAll method cannot be bound. |
示例:
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let map: LightWeightMap<string, number> = new LightWeightMap();
map.set("sparrow", 356);
let result = lightWeightMap.hasAll(map);
hasKey
hasKey(key: K): boolean
判断此LightWeightMap中是否含有该指定key。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
key | K | 是 | 指定key。 |
返回值:
类型 | 说明 |
---|---|
boolean | 包含指定key返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The hasKey method cannot be bound. |
示例:
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
let result = lightWeightMap.hasKey("squirrel");
hasValue
hasValue(value: V): boolean
判断此LightWeightMap中是否含有该指定value。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | V | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 包含指定元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The hasValue method cannot be bound. |
示例:
let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
let result = lightWeightMap.hasValue(123);
increaseCapacityTo
increaseCapacityTo(minimumCapacity: number): void
将当前LightWeightMap扩容至可以容纳指定数量元素。
系统能力: SystemCapability.Utils.Lang
参数:<