鸿蒙语言基础类库:【@ohos.util.HashMap (非线性容器HashMap)】
非线性容器HashMap
说明: 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。开发前请熟悉鸿蒙开发指导文档:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。
HashMap底层使用数组+链表+红黑树的方式实现,查询、插入和删除的效率都很高。HashMap存储内容基于key-value的键值对映射,不能有重复的key,且一个key只能对应一个value。
HashMap和[TreeMap]相比,HashMap依据键的hashCode存取数据,访问速度较快。而TreeMap是有序存取,效率较低。
[HashSet]基于HashMap实现。HashMap的输入参数由key、value两个值组成。在HashSet中,只对value对象进行处理。
推荐使用场景: 需要快速存取、删除以及插入键值对数据时,推荐使用HashMap。
导入模块
import HashMap from @ohos.util.HashMap; 1HashMap
属性系统能力: SystemCapability.Utils.Lang
constructor()
HashMap的构造函数。
系统能力: SystemCapability.Utils.Lang
示例:
let hashMap = new HashMap(); 1 isEmptyisEmpty(): boolean
判断该HashMap是否为空。
系统能力: SystemCapability.Utils.Lang
返回值:
示例:
const hashMap = new HashMap(); let result = hashMap.isEmpty(); 12 )hasKeyhasKey(key: K): boolean
判断此HashMap中是否含有该指定key。
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); let result = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); let result1 = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 1234 hasValuehasValue(value: V): boolean
判断此HashMap中是否含有该指定value。
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); let result = hashMap.hasValue(123); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); let result1 = hashMap.hasValue(123); 1234 getget(key: K): V
获取指定key所对应的value。
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); let result = hashMap.get("sdfs"); 1234 setAllsetAll(map: HashMap<K, V>): void
将一个HashMap中的所有元素组添加到另一个hashMap中。
系统能力: SystemCapability.Utils.Lang
参数:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); let newHashMap = new HashMap(); hashMap.setAll(newHashMap); 12345 setset(key: K, value: V): Object
向HashMap中添加一组数据。
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); let result = hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); 12 removeremove(key: K): V
删除指定key所对应元素。
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); let result = hashMap.remove("sdfs"); 1234 clearclear(): void
清除HashMap中的所有元素,并把length置为0。
系统能力: SystemCapability.Utils.Lang
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); hashMap.clear(); 1234 keyskeys(): IterableIterator
返回包含此映射中包含的键名的新迭代器对象。
系统能力: SystemCapability.Utils.Lang
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); let iter = hashMap.keys(); let temp = iter.next().value; while(temp != undefined) { console.log("value:" + temp); temp = iter.next().value; } 123456789 valuesvalues(): IterableIterator
返回包含此映射中包含的键值的新迭代器对象。
系统能力: SystemCapability.Utils.Lang
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); let iter = hashMap.values(); let temp = iter.next().value; while(temp != undefined) { console.log("value:" + temp); temp = iter.next().value; } 123456789 replacereplace(key: K, newValue: V): boolean
系统能力: SystemCapability.Utils.Lang
参数:
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("sdfs", 123); let result = hashMap.replace("sdfs", 357); 123 forEachforEach(callbackfn: (value?: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void
通过回调函数来遍历HashMap实例对象上的元素以及元素对应的下标。
系统能力: SystemCapability.Utils.Lang
参数:
callbackfn的参数说明:
示例:
let hashMap = new HashMap(); hashMap.set("sdfs", 123); hashMap.set("dfsghsf", 357); hashMap.forEach((value, key) => { console.log("value:" + value, key); }); 123456 entriesentries(): IterableIterator<[K, V]>
返回包含此映射中包含的键值对的新迭代器对象。
系统能力: SystemCapability.Utils.Lang
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 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; } 12345678910 [Symbol.iterator]Symbol.iterator: IterableIterator<[K, V]>
返回一个迭代器,迭代器的每一项都是一个 JavaScrIPt 对象,并返回该对象。
系统能力: SystemCapability.Utils.Lang
返回值:
示例:
let hashMap = new HashMap(); hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123); hashMap.set("sdfs", 356); // 使用方法一: for (let item of hashMap) { console.log("key:" + item[0]); console.log("value:" + item[1]); } // 使用方法二: let iter = hashMap[Symbol.iterator](); let temp = iter.next().value; while(temp != undefined) { console.log("key:" + temp[0]); console.log("value:" + temp[1]); temp = iter.next().value; }Ongwu博客 版权声明:以上内容未经允许不得转载!授权事宜或对内容有异议或投诉,请联系站长,将尽快回复您,谢谢合作!