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

HashMap底层使用数组+链表+红黑树的方式实现,查询、插入和删除的效率都很高。HashMap存储内容基于key-value的键值对映射,不能有重复的key,且一个key只能对应一个value。

HashMap和[TreeMap]相比,HashMap依据键的hashCode存取数据,访问速度较快。而TreeMap是有序存取,效率较低。

HashSet\]基于HashMap实现。HashMap的输入参数由key、value两个值组成。在HashSet中,只对value对象进行处理。 **推荐使用场景:** 需要快速存取、删除以及插入键值对数据时,推荐使用HashMap。 文档中存在泛型的使用,涉及以下泛型标记符: * K:Key,键 * V:Value,值 **说明:** > 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > 鸿蒙开发指导文档: [gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md")点击或者复制转到。 ### 导入模块 ```cpp import HashMap from '@ohos.util.HashMap'; ``` ### HashMap #### 属性 **系统能力:** SystemCapability.Utils.Lang | 名称 | 类型 | 可读 | 可写 | 说明 | |--------|--------|----|----|---------------| | length | number | 是 | 否 | HashMap的元素个数。 | #### constructor constructor() HashMap的构造函数。 **系统能力:** SystemCapability.Utils.Lang **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-------------------------------------------------------| | 10200012 | The HashMap's constructor cannot be directly invoked. | **示例:** ```cpp let hashMap = new HashMap(); ``` #### isEmpty isEmpty(): boolean 判断该HashMap是否为空。 **系统能力:** SystemCapability.Utils.Lang **返回值:** | 类型 | 说明 | |---------|----------------------| | boolean | 为空返回true,不为空返回false。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-------------------------------------| | 10200011 | The isEmpty method cannot be bound. | **示例:** ```cpp const hashMap = new HashMap(); let result = hashMap.isEmpty(); ``` #### hasKey hasKey(key: K): boolean 判断此HashMap中是否含有该指定key。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-----|----|----|--------| | key | K | 是 | 指定Key。 | **返回值:** | 类型 | 说明 | |---------|--------------------------| | boolean | 包含指定Key返回true,否则返回false。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|------------------------------------| | 10200011 | The hasKey method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); let result = hashMap.hasKey("squirrel"); ``` #### hasValue hasValue(value: V): boolean 判断此HashMap中是否含有该指定value。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-------|----|----|----------| | value | V | 是 | 指定value。 | **返回值:** | 类型 | 说明 | |---------|----------------------------| | boolean | 包含指定value返回true,否则返回false。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|--------------------------------------| | 10200011 | The hasValue method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); let result = hashMap.hasValue(123); ``` #### get get(key: K): V 获取指定key所对应的value,不存在返回undefined。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-----|----|----|-----------| | key | K | 是 | 查找的指定key。 | **返回值:** | 类型 | 说明 | |----|-----------------| | V | 返回key映射的value值。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|---------------------------------| | 10200011 | The get method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let result = hashMap.get("sparrow"); ``` #### setAll setAll(map: HashMap\): void 将一个HashMap中的所有元素组添加到另一个hashMap中。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-----|-----------------|----|----------------| | map | HashMap\ | 是 | 被添加元素的hashMap。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|------------------------------------| | 10200011 | The setAll method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let newHashMap = new HashMap(); newHashMap.set("newMap", 99); hashMap.setAll(newHashMap); ``` #### set set(key: K, value: V): Object 向HashMap中添加或更新一组数据。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-------|----|----|---------------| | key | K | 是 | 添加或更新成员数据的键名。 | | value | V | 是 | 添加或更新成员数据的值。 | **返回值:** | 类型 | 说明 | |--------|----------------| | Object | 返回添加后的hashMap。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|---------------------------------| | 10200011 | The set method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); let result = hashMap.set("squirrel", 123); ``` #### remove remove(key: K): V 删除指定key所对应元素。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |-----|----|----|--------| | key | K | 是 | 指定key。 | **返回值:** | 类型 | 说明 | |----|-----------| | V | 返回删除元素的值。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|------------------------------------| | 10200011 | The remove method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let result = hashMap.remove("sparrow"); ``` #### clear clear(): void 清除HashMap中的所有元素,并把length置为0。 **系统能力:** SystemCapability.Utils.Lang **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-----------------------------------| | 10200011 | The clear method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); hashMap.clear(); ``` #### keys keys(): IterableIterator\ 返回包含此映射中包含的键的新迭代器对象。 **系统能力:** SystemCapability.Utils.Lang **返回值:** | 类型 | 说明 | |-----------------------|----------| | IterableIterator\ | 返回一个迭代器。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|----------------------------------| | 10200011 | The keys method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let iter = hashMap.keys(); let temp = iter.next().value; while(temp != undefined) { console.log("value:" + temp); temp = iter.next().value; } ``` #### values values(): IterableIterator\ 返回包含此映射中包含的键对应的值的新迭代器对象。 **系统能力:** SystemCapability.Utils.Lang **返回值:** | 类型 | 说明 | |-----------------------|----------| | IterableIterator\ | 返回一个迭代器。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|------------------------------------| | 10200011 | The values method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let iter = hashMap.values(); let temp = iter.next().value; while(temp != undefined) { console.log("value:" + temp); temp = iter.next().value; } ``` #### replace replace(key: K, newValue: V): boolean 对HashMap中一组数据进行更新(替换)。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |----------|----|----|---------------| | key | K | 是 | 依据key指定替换的元素。 | | newValue | V | 是 | 替换成员数据的值。 | **返回值:** | 类型 | 说明 | |---------|-----------------------------------| | boolean | 是否成功对已有数据进行替换,成功返回true,失败返回false。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-------------------------------------| | 10200011 | The replace method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("sparrow", 123); let result = hashMap.replace("sparrow", 357); ``` #### forEach forEach(callbackFn: (value?: V, key?: K, map?: HashMap\) =\> void, thisArg?: Object): void 通过回调函数来遍历HashMap实例对象上的元素以及元素对应的下标。 **系统能力:** SystemCapability.Utils.Lang **参数:** | 参数名 | 类型 | 必填 | 说明 | |------------|----------|----|------------------------| | callbackFn | function | 是 | 回调函数。 | | thisArg | Object | 否 | callbackfn被调用时用作this值。 | callbackfn的参数说明: | 参数名 | 类型 | 必填 | 说明 | |-------|-----------------|----|---------------------| | value | V | 否 | 当前遍历到的元素键值对的值。 | | key | K | 否 | 当前遍历到的元素键值对的键。 | | map | HashMap\ | 否 | 当前调用forEach方法的实例对象。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-------------------------------------| | 10200011 | The forEach method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("sparrow", 123); hashMap.set("gull", 357); hashMap.forEach((value, key) => { console.log("value:" + value, "key:" + key); }); ``` #### entries entries(): IterableIterator\<\[K, V\]\> 返回包含此映射中包含的键值对的新迭代器对象。 **系统能力:** SystemCapability.Utils.Lang **返回值:** | 类型 | 说明 | |------------------------------|----------| | IterableIterator\<\[K, V\]\> | 返回一个迭代器。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 | |----------|-------------------------------------| | 10200011 | The entries method cannot be bound. | **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); let iter = hashMap.entries(); let temp = iter.next().value; while(temp != undefined) { console.log("key:" + temp[0]); console.log("value:" + temp[1]); temp = iter.next().value; } ``` #### \[Symbol.iterator

Symbol.iterator\](): IterableIterator\<\[K, V\]\> 返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 **系统能力:** SystemCapability.Utils.Lang **返回值:** | 类型 | 说明 | |------------------------------|----------| | IterableIterator\<\[K, V\]\> | 返回一个迭代器。 | **错误码:** 以下错误码的详细介绍请参见[语言基础类库错误码](https://gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md "语言基础类库错误码")。 | 错误码ID | 错误信息 `HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿` | |----------|---------------------------------------------------| | 10200011 | The Symbol.iterator method cannot be bound. | ![搜狗高速浏览器截图20240326151450.png](https://file.jishuzhan.net/article/1783524160509579266/e6dfdcc17aaf87596b0d8d8d57904f6a.webp) **示例:** ```cpp let hashMap = new HashMap(); hashMap.set("squirrel", 123); hashMap.set("sparrow", 356); // 使用方法一: for (let item of hashMap) { console.log("key:" + item[0]); console.log("value:" + item[1]); } // 使用方法二: let iter = hashMap[Symbol.iterator](); let temp: IteratorResult = iter.next(); while(!temp.done) { console.log("key:" + temp.value[0]); console.log("value:" + temp.value[1]); temp = iter.next(); } ``` ## 鸿蒙Next核心技术分享 ### 1、鸿蒙基础知识←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/1d2aa7ea03466c25d35f29cb9c0d7b08.webp) ### 2、鸿蒙ArkUI←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ### ![](https://file.jishuzhan.net/article/1783524160509579266/c2d3841c09109a882bd488536d020838.webp) ### 3、鸿蒙进阶技术←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ### ![](https://file.jishuzhan.net/article/1783524160509579266/5966f527623bf208b2a6020d1ea58ea2.webp) ### 4、鸿蒙就业高级技能←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/bac18a163bccd726e496e333f4e93583.webp) ### 5、鸿蒙多媒体技术←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/16e5ec25bf5c306b062cdc2eb3a20a73.webp) ### 6、鸿蒙南向驱动开发←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/47bbbd9d10860d321d66009dc2a58508.webp) ### 7、鸿蒙南向内核设备开发←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/e05fd06ec46f202f4602b8e87e9af129.webp) ### 8、鸿蒙系统裁剪与移植←[《鸿蒙NEXT星河版开发学习文档》](https://docs.qq.com/doc/DUlNzQU5CV1JmUGlG "《鸿蒙NEXT星河版开发学习文档》") ![](https://file.jishuzhan.net/article/1783524160509579266/5b0ea7565a22af4cce9107610df9d2f0.webp)

相关推荐
万少9 小时前
8 月中 汇报下近半个月都在做些什么
程序员
小小小小小星14 小时前
鸿蒙开发核心功能模块全解析:从架构到实战应用
harmonyos
奶糖不太甜15 小时前
鸿蒙开发问题之纯血鸿蒙自启动步骤详解
harmonyos
舒一笑15 小时前
Mac 上安装并使用 frpc(FRP 内网穿透客户端)指南
后端·网络协议·程序员
AI大模型15 小时前
强推!大模型学习书籍合集推荐 | (含PDF地址)
程序员·llm·agent
xq952720 小时前
鸿蒙next 获取versionCode和versionName
harmonyos
鸿蒙小白龙20 小时前
openharmony之恢复出厂设置需求总结
harmonyos·鸿蒙·鸿蒙系统
污橘21 小时前
Nginx反向代理Oracle
后端·程序员
深海的鲸同学 luvi21 小时前
【HarmonyOS】H5 实现在浏览器中正常跳转 AppLinking 至应用
华为·harmonyos