OpenHarmony语言基础类库【@ohos.util.LightWeightMap (非线性容器LightWeightMap)】

news2024/10/6 12:35:41

 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的元素个数。

鸿蒙开发指导文档:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

constructor

constructor()

LightWeightMap的构造函数。

系统能力:  SystemCapability.Utils.Lang

错误码:

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

错误码ID错误信息
10200012The LightWeightMap's constructor cannot be directly invoked.

示例:

let lightWeightMap = new LightWeightMap();

isEmpty

isEmpty(): boolean

判断该LightWeightMap是否为空。

系统能力:  SystemCapability.Utils.Lang

返回值:

类型说明
boolean为空返回true,不为空返回false。

错误码:

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

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

示例:

const lightWeightMap = 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 = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let map = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let map = 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 = 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 = 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 = 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 = 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 = 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 = 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 = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
  console.log("value:" + temp);
  temp = iter.next().value;
}

values

values(): IterableIterator<V>

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

系统能力:  SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

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

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 = new LightWeightMap();
lightWeightMap.set("sparrow", 123);
lightWeightMap.set("gull", 357);
lightWeightMap.forEach((value, key) => {
    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 = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let iter = lightWeightMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
  console.log("key:" + temp[0]);
  console.log("value:" + temp[1]);
  temp = iter.next().value;
}

toString

toString(): String

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

系统能力:  SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

let lightWeightMap = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);
let result = lightWeightMap.toString();

[Symbol.iterator]

搜狗高速浏览器截图20240326151450.png

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

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

系统能力:  SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

let lightWeightMap = new LightWeightMap();
lightWeightMap.set("squirrel", 123);
lightWeightMap.set("sparrow", 356);

// 使用方法一:
for (let item of lightWeightMap) { 
  console.log("key:" + item[0]);
  console.log("value:" + item[1]);
}

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

鸿蒙Next核心技术分享

1、鸿蒙基础知识←《鸿蒙NEXT星河版开发学习文档》

2、鸿蒙ArkUI←《鸿蒙NEXT星河版开发学习文档》

3、鸿蒙进阶技术←《鸿蒙NEXT星河版开发学习文档》

 4、鸿蒙就业高级技能←《鸿蒙NEXT星河版开发学习文档》 

 5、鸿蒙多媒体技术←《鸿蒙NEXT星河版开发学习文档》 

6、鸿蒙南向驱动开发←《鸿蒙NEXT星河版开发学习文档》  

7、鸿蒙南向内核设备开发←《鸿蒙NEXT星河版开发学习文档》  

 8、鸿蒙系统裁剪与移植←《鸿蒙NEXT星河版开发学习文档》  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1627057.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++中的queue(容器适配器)

目录 一、成员函数 一、构造函数 二、入栈 push 三、出栈 pop 四、判空 empty 五、队列大小 size 六、取队头元素 front 七、取队尾元素 back 八、入栈 emplace 九、交换函数 swap 二、非成员函数重载 一、关系运算符重载 二、交换函数 swap C中的queue不再是容…

pytest-xdist:远程多主机 - 分布式运行自动化测试

简介&#xff1a;pytest-xdist插件使用新的测试执行模式扩展了pytest&#xff0c;最常用的是在多个CPU之间分发测试以加快测试执行&#xff0c;即 pytest -n auto同时也是一个非常优秀的分布式测试插件&#xff0c;分别支持ssh和socket两种方式实现master和worker的远程通讯。…

Java8 Stream常见用法

Stream流的常见用法&#xff1a; 1.利用stream流特性把数组转list集合 //定义一个数组Integer[] array {5,2,1,6,4,3};//通过stream特性把数组转list集合List<Integer> list Arrays.stream(array).collect(Collectors.toList());//打印结果System.out.println(list);…

Ubuntu16.04搭建webrtc服务器

本人查阅无数资料,历时3周搭建成功 一、服务器组成 AppRTC 房间+Web服务器 https://github.com/webrtc/apprtcCollider 信令服务器,在AppRTC源码里CoTurn coturn打洞+中继服务器 Nginx 服务器,用于Web访问代理和Websocket代理。AppRTC 房间+Web服务器使用python+js语言 App…

两大成果发布!“大规模量子云算力集群”和高性能芯片展示中国科技潜力

在当前的科技领域&#xff0c;量子计算的进步正日益引起全球的关注。中国在这一领域的进展尤为显著&#xff0c;今天&#xff0c;北京量子信息科学研究院&#xff08;以下简称北京量子院&#xff09;和中国科学院量子信息与量子科技创新研究院&#xff08;以下简称量子创新院&a…

2024年度延安市农业农村领域科技创新研发平台申报类别程序、相关要求

一、征集类别 此次征集类别包括市级农业科技园区、星创天地、县域科技创新试验示范站、科技示范镇、乡村振兴科技示范村。 二、申报程序 1.农业科技园区由乡(镇)人民政府牵头申报,经县(市、区)科技管理部门审核后向市科技局推荐报送。(申请模板见附件1)。 2.县域科技创新试…

Unreal Engine子类化系统UButton

UE系统Button点击事件无法传递参数&#xff0c;通过子类化系统Button添加自定义参数扩展实现Button点击事件参数传递点击C类文件夹&#xff0c;在右边的区域点击鼠标右键&#xff0c;在弹出的菜单中选择“新建C类”在弹出的菜单中选中“显示所有类”&#xff0c;选择Button作为…

DSP开发实战教程--EPWM模块的影子寄存器详细讲解原理和代码实例

EPWM模块影子寄存器的原理 在TI&#xff08;Texas Instruments&#xff09;的DSP28335中&#xff0c;EPWM&#xff08;Enhanced Pulse Width Modulator&#xff09;模块提供了高精度、高灵活性的PWM信号生成功能。为了能在不影响当前PWM波形输出的情况下预装载新的PWM参数&…

5.Labview簇、变体与类(下) -- 项目与Labview类的使用

本文介绍Labview类的使用&#xff0c;在Labview中&#xff0c;何为类&#xff1f;应该如何理解&#xff1f;具体有什么应用场景&#xff1f; 本文基于Labview软件&#xff0c;讲解了类函数的使用方法和场景&#xff0c;从理论上讲解其数据流的底层概念&#xff0c;从实践上建立…

机器人前馈控制MATLAB实现

在机器人控制中&#xff0c;前馈控制是一种常用的方法&#xff0c;用于补偿系统中的已知动态。前馈控制通常与反馈控制结合使用&#xff0c;以提高系统的跟踪性能和响应速度。在MATLAB中实现机器人前馈控制涉及几个步骤&#xff0c;包括系统建模、设计前馈控制器、实现控制算法…

Docker 入门篇(二)-- Linux 环境离线安装

引言 docker 系列文章&#xff1a; Docker 入门篇&#xff08;一&#xff09;-- 简介与安装教程&#xff08;Windows和Linux&#xff09; 一、安装环境准备 centos &#xff1a;CentOS Linux release 7.6.1810 (Core)docker 版本&#xff1a;docker-26.1.0.tgz 官网下载地址…

【算法基础实验】图论-基于DFS的连通性检测

基于DFS的连通性检测 理论基础 在图论中&#xff0c;连通分量是无向图的一个重要概念&#xff0c;特别是在处理图的结构和解析图的组成时。连通分组件表示图中的一个子图&#xff0c;在这个子图中任意两个顶点都是连通的&#xff0c;即存在一条路径可以从一个顶点到达另一个顶…

理解归并排序的两种方法(超详细)

目录 前言 一.方法一&#xff1a;归并排序 1.1 归并思路 1.1.1 递归(分解) 1.1.2 区间(排序) 1.1.3 合并拷贝回原数组(合并) 二.归并排序过程 2.1 递归(分解)图解 2.2 归并有序区间(排序)图解 2.2.1 单独一趟排序 2.2.2 有序区间递归排序 2.2.3 数组拷贝(合并) 2.3 归并全部代码…

开箱机选型攻略:如何挑选适合你的自动化设备?

在如今快节奏的生产环境中&#xff0c;自动化设备的运用已成为企业提升效率、降低成本的关键。开箱机作为自动化生产线上的重要一环&#xff0c;其选型对于企业来说至关重要。星派将为您提供一份开箱机选型攻略&#xff0c;帮助您挑选出最适合自己的自动化设备。 一、了解开箱…

从 Apache Doris 到 SelectDB Cloud:云原生架构下的弹性能力揭秘

随着云时代的到来&#xff0c;越来越多企业开始在公有云、私有云乃至 K8s 容器平台构建实时数据平台。云计算基础设施的革新&#xff0c;促使着数据仓库朝着云原生的方向发展。而用户日益复杂的业务负载和降本增效的需求&#xff0c;对于系统资源的精细化管理和成本效益等方面提…

一种利用合法工具渗透的新型方法

摘要 黑客在执行各种攻击步骤时倾向于优先选择合法工具&#xff0c;因为这些工具能帮助他们规避检测系统&#xff0c;同时将恶意软件开发成本降至最低。网络扫描、捕获进程内存转储、数据外泄、远程运行文件&#xff0c;甚至加密驱动器——所有这些都可以通过可信软件完成。为了…

ubuntu查看opencveigen

ubuntu查看opencv&eigen&cmake版本的方法 eigen eigen版本号在/usr/include/eigen3/Eigen/src/Core/util/Macros.h文件中&#xff0c;下图代表版本3.3.7 opencv版本 pkg-config --modversion opencv4也可能最后的字符串是opencv2&#xff0c;opencv

R基本的数据管理

一&#xff0c;创建变量 创建一个数据框 > myData<-data.frame(x1c(1,2,3,4,5,6),x2c(6,5,67,8,9,0)) > myDatax1 x2 1 1 6 2 2 5 3 3 67 4 4 8 5 5 9 6 6 0增加一列为两者的和 > myData$sum<-myData$x1myData$x2 > myDatax1 x2 sum 1 1 6 …

3d合并的模型为什么没有模型---模大狮模型网

在3D建模中&#xff0c;合并模型是常见的操作&#xff0c;它可以将多个模型合并成一个整体。然而&#xff0c;有时候在合并后却发现部分模型消失了&#xff0c;这可能会让人感到困惑和失望。本文将探讨为什么合并的3D模型中会出现没有模型的情况&#xff0c;并提供一些解决方法…

【Unity动画系统】动画基本原理与Avater骨骼复用

动画基本原理 动画片段文件是一个描述物体变化状态的文本文件 在Unity中创建的资源文件大多都是YAML语言编写的文本文件 Curves表示一种变化状态&#xff0c;为空的话则没有记录任何内容 位置变化后的旋转变化状态&#xff1a; 动画文件里的Path名字要相同才能播放相同的动画 …