当前位置: 首页 > article >正文

HarmonyOS 非线性容器LightWeightMap 常用的几个方法

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

LightWeightMap依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。

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

LightWeightMap和HashMap都是用来存储键值对的集合,LightWeightMap占用内存更小。

推荐使用场景: 当需要存取key-value键值对时,推荐使用占用内存更小的LightWeightMap。

HarmonyOS 非线性容器LightWeightMap  的好处 

内存占用小

  • LightWeightMap采用轻量级结构,相较于其他键值对存储容器(如HashMap),在存储相同数量的键值对时,LightWeightMap占用的内存更小。
  • 这使得LightWeightMap在内存敏感的场景中具有显著优势,如移动设备或嵌入式系统中的应用开发。

2. 高效的键值对存储与查找

  • LightWeightMap依据泛型定义,通过hash算法实现键值的快速查找。
  • 它使用一个数组存储hash值,然后映射到其他数组中的键值对,这种设计使得查找效率较高。

3. 灵活的扩容机制

  • LightWeightMap的初始默认容量大小为8,每次扩容大小为原始容量的两倍。
  • 这种灵活的扩容机制确保了LightWeightMap在存储大量数据时仍能保持较高的性能,同时避免了频繁的扩容操作带来的开销。

4. 适用于多种场景

  • 当需要存取key-value键值对时,LightWeightMap是一个很好的选择。
  • 它的轻量级结构和高效性能使得它在各种应用场景中都能发挥出色的表现,如缓存管理、数据映射等。

5. 易于使用与集成

  • LightWeightMap提供了简洁的API接口,使得开发者可以方便地对其进行增、删、改、查等操作。
  • 同时,LightWeightMap可以轻松地与其他HarmonyOS组件和模块进行集成,为开发者提供了更多的灵活性和便利性。

 

LightWeightMap 

1. constructor

2. isEmpty

3. hasAll

4. hasKey

5. hasValue

6. get

7. set

8. remove

9. setAll

导入模块

import { LightWeightMap } from '@kit.ArkTS';

LightWeightMap

属性

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

1. constructor

constructor()

LightWeightMap的构造函数。

使用发送:

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

 

2. isEmpty

isEmpty(): boolean

判断该LightWeightMap是否为空。

返回值:

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

使用方式:

const lightWeightMap: LightWeightMap<string, number> = new LightWeightMap();
let result = lightWeightMap.isEmpty();

3. hasAll

hasAll(map: LightWeightMap<K, V>): boolean

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

参数:

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

返回值:

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

使用方式:

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);

4. hasKey

hasKey(key: K): boolean

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

参数:

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

返回值:

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

使用方式: 

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

5. hasValue

hasValue(value: V): boolean

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

参数:

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

返回值:

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

使用方式: 

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

6. get

get(key: K): V

获取指定key所对应的value。

参数:

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

返回值:

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

使用方式: 

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

7. set

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

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

参数:

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

返回值:

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

使用方式: 

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

8. remove

remove(key: K): V

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

参数:

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

返回值:

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

使用方式: 

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

9. setAll

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

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

参数:

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

 使用方式: 

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中

 如需要其他方法 请参考官方文档

制作不易 点个关注再走吧。°(°¯᷄◠¯᷅°)°。


http://www.kler.cn/a/440863.html

相关文章:

  • 学习笔记:Verilog连续赋值及在线仿真
  • React Native状态管理器Redux、MobX、Context API、useState
  • RPC 服务与 gRPC 的入门案例
  • Docker的存储卷
  • 鸿蒙Next MVVM思想总结
  • 渗透测试之js利用
  • Redis查询占用空间最大的10个key
  • 在pycharm2024.3.1中配置anaconda3-2024-06环境
  • 005 Qt常用控件Qwidget_下
  • 【研究趋势】Nips2024 多模态论文热点分析
  • Anaconda更改虚拟环境安装路径
  • RPC远程服务调用详解和gRPC简介
  • 【合作原创】使用Termux搭建可以使用的生产力环境(七)
  • springmvc跳转不经过视图解析器,controller保存数据,controller层返回json数据,拦截器,全局异常处理,文件上传含本地和阿里oss
  • Fscan内网横向工具
  • Ansible Playbook 简介
  • 三维移动游戏
  • C++智能指针详解
  • 3D Gaussian Splatting for Real-Time Radiance Field Rendering-简洁版
  • 05、SpringMVC全注解开发