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

名称类型可读可写说明
lengthnumberLightWeightMap的元素个数。

constructor

constructor()

LightWeightMap的构造函数。

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200012The 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错误信息
10200011The 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

参数:

参数名类型必填说明
mapLightWeightMap<K, V>比较对象。

返回值:

类型说明
boolean包含所有元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The 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

参数:

参数名类型必填说明
keyK指定key。

返回值:

类型说明
boolean包含指定key返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The 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

参数:

参数名类型必填说明
valueV指定元素。

返回值:

类型说明
boolean包含指定元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The 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

参数:

参数名类型必填说明
minimumCapacitynumber需要容纳的数量。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The increaseCapacityTo method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.increaseCapacityTo(10);

get

get(key: K): V

获取指定key所对应的value。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
keyK指定key。

返回值:

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

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The get method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.get("sparrow");

getIndexOfKey

getIndexOfKey(key: K): number

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
keyK被查找的元素。

返回值:

类型说明
number返回key元素第一次出现时的下标值,查找失败返回-1。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The getIndexOfKey method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfKey("sparrow");

getIndexOfValue

getIndexOfValue(value: V): number

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
valueV被查找的元素。

返回值:

类型说明
number返回value元素第一次出现时的下标值,查找失败返回-1。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The getIndexOfValue method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getIndexOfValue(123);

getKeyAt

getKeyAt(index: number): K

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
indexnumber所查找的下标。

返回值:

类型说明
K返回该下标对应的元素键值对中key值。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The getKeyAt method cannot be bound.
10200001The value of index is out of range.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getKeyAt(1);

setAll

setAll(map: LightWeightMap<K, V>): void

将一个LightWeightMap中的所有元素组添加到另一个lightWeightMap中。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
mapLightWeightMap<K, V>被添加元素的lightWeightMap。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The setAll 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.setAll(lightWeightMap); // 将lightWeightMap中所有的元素添加到map中

set

set(key: K, value: V): Object

向LightWeightMap中添加或更新一组数据。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
keyK添加成员数据的键名。
valueV添加成员数据的值。

返回值:

类型说明
Object返回添加数据后的lightWeightMap。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The set method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
let result = lightWeightMap.set("squirrel", 123);

remove

remove(key: K): V

删除并返回指定key映射的元素。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
keyK指定key。

返回值:

类型说明
V返回删除元素的值。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The remove method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.remove("sparrow");

removeAt

removeAt(index: number): boolean

删除指定下标对应的元素。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
indexnumber指定下标。

返回值:

类型说明
boolean成功删除元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The removeAt method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.removeAt(1);

setValueAt

setValueAt(index: number, newValue: V): boolean

替换指定下标对应键值对中的元素。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
indexnumber指定下标。
newValueV替换键值对中的值。

返回值:

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

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The setValueAt method cannot be bound.
10200001The value of index is out of range.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.setValueAt(1, 3546);

getValueAt

getValueAt(index: number): V

获取指定下标对应键值对中的元素。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
indexnumber指定下标。

返回值:

类型说明
V返回指定下标对应键值对中的元素。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The getValueAt method cannot be bound.
10200001The value of index is out of range.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.getValueAt(1);

clear

clear(): void

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

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The clear method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
lightWeightMap.clear();

keys

keys(): IterableIterator<K>

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
IterableIterator<K>返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The keys method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.keys();
let temp: IteratorResult<string, number> = iter.next();
while(!temp.done) {
  console.log("value:" + temp.value);
  temp = iter.next();
}

values

values(): IterableIterator<V>

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
IterableIterator<V>返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The values method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.values();
let temp: IteratorResult<number> = iter.next();
while(!temp.done) {
  console.log("value:" + temp.value);
  temp = iter.next();
}

forEach

forEach(callbackFn: (value?: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
callbackFnfunction回调函数。
thisArgObjectcallbackfn被调用时用作this值,默认值为当前实例对象。

callbackfn的参数说明:

参数名类型必填说明
valueV当前遍历到的元素键值对的值,默认值为首个键值对的值。
keyK当前遍历到的元素键值对的键,默认值为首个键值对的键。
mapLightWeightMap<K, V>当前调用forEach方法的实例对象,默认值为当前实例对象。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The forEach method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("gull", 357);
lightWeightMap.forEach((value?: number, key?: string) => {
  console.log("value:" + value, "key:" + key);
});

entries

entries(): IterableIterator<[K, V]>

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The entries method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.entries();
let temp: IteratorResult<Object[]> = iter.next();
while(!temp.done) {
  console.log("key:" + temp.value[0]);
  console.log("value:" + temp.value[1]);
  temp = iter.next();
}

toString

toString(): String

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
String返回一个字符串。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The toString method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.toString();

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<[K, V]>

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

说明:

本接口不支持在.ets文件中使用

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The Symbol.iterator method cannot be bound.

示例:

let lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);

// 使用方法一:
let nums = Array.from(lightWeightMap.values());
for (let item1 of nums) {
  console.log("value:" + item1);
}

let key = Array.from(lightWeightMap.keys());
for (let item2 of key) {
  console.log("key:" + item2);
}

// 使用方法二:
let iter = lightWeightMap[Symbol.iterator]();
let temp: IteratorResult<Object[]> = iter.next();
while(!temp.done) {
  console.log("key:" + temp.value[0]);
  console.log("value:" + temp.value[1]);
  temp = iter.next();
}

最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,

内容包含了:ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

如果你是一名有经验的资深Android移动开发、Java开发、前端开发、对鸿蒙感兴趣以及转行人员,可以直接领取这份资料

 获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

鸿蒙(HarmonyOS NEXT)最新学习路线

  •  HarmonOS基础技能

  • HarmonOS就业必备技能 
  •  HarmonOS多媒体技术

  • 鸿蒙NaPi组件进阶

  • HarmonOS高级技能

  • 初识HarmonOS内核 
  • 实战就业级设备开发

 有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

《鸿蒙 (OpenHarmony)开发入门教学视频》

《鸿蒙生态应用开发V2.0白皮书》

图片

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

图片

 《鸿蒙开发基础》

  • ArkTS语言
  • 安装DevEco Studio
  • 运用你的第一个ArkTS应用
  • ArkUI声明式UI开发
  • .……

图片

 《鸿蒙开发进阶》

  • Stage模型入门
  • 网络管理
  • 数据管理
  • 电话服务
  • 分布式应用开发
  • 通知与窗口管理
  • 多媒体技术
  • 安全技能
  • 任务管理
  • WebGL
  • 国际化开发
  • 应用测试
  • DFX面向未来设计
  • 鸿蒙系统移植和裁剪定制
  • ……

图片

《鸿蒙进阶实战》

  • ArkTS实践
  • UIAbility应用
  • 网络案例
  • ……

图片

 获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

总结

总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

  • 24
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值