HarmonyOS实战开发:@ohos.util.LightWeightMap (非线性容器LightWeightMap)

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

参数:<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值