HashSet基于[HashMap]实现。在HashSet中,只对value对象进行处理。
HashSet和[TreeSet]相比,HashSet中的数据无序存放,即存放元素的顺序和取出的顺序不一致,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不建议存放null值,可能会对排序结果产生影响。
推荐使用场景: 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。
文档中存在泛型的使用,涉及以下泛型标记符:
- T:Type,类
说明:
导入模块
import HashSet from '@ohos.util.HashSet';
鸿蒙开发指导文档:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]点击或者复制转到。
HashSet
属性
系统能力: SystemCapability.Utils.Lang
名称 | 类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
length | number | 是 | 否 | HashSet的元素个数。 |
示例:
let hashSet = new HashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
let res = hashSet.length;
constructor
constructor()
HashSet的构造函数。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200012 | The HashSet's constructor cannot be directly invoked. |
示例:
let hashSet = new HashSet();
isEmpty
isEmpty(): boolean
判断该HashSet是否为空。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
boolean | 为空返回true,不为空返回false。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The isEmpty method cannot be bound. |
示例:
const hashSet = new HashSet();
let result = hashSet.isEmpty();
has
has(value: T): boolean
判断此HashSet中是否含有该指定元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 包含指定元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The has method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
let result = hashSet.has("squirrel");
add
add(value: T): boolean
向HashSet中添加数据。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | T | 是 | 添加成员数据。 |
返回值:
类型 | 说明 |
---|---|
boolean | 成功增加元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The add method cannot be bound. |
示例:
let hashSet = new HashSet();
let result = hashSet.add("squirrel");
remove
remove(value: T): boolean
删除指定的元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | T | 是 | 指定删除的元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 成功删除指定元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The remove method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let result = hashSet.remove("sparrow");
clear
clear(): void
清除HashSet中的所有元素,并把length置为0。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The clear method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
hashSet.clear();
values
values(): IterableIterator
返回包含此映射中包含的键值的新迭代器对象。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
IterableIterator | 返回一个迭代器。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The values method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let iter = hashSet.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log("value:" + temp);
temp = iter.next().value;
}
forEach
forEach(callbackFn: (value?: T, key?: T, set?: HashSet) => void, thisArg?: Object): void
通过回调函数来遍历实例对象上的元素以及元素对应的下标。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
callbackFn | function | 是 | 回调函数。 |
thisArg | Object | 否 | callbackfn被调用时用作this值。 |
callbackfn的参数说明:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | T | 否 | 当前遍历到的元素键值对的值。 |
key | T | 否 | 当前遍历到的元素键值对的值(和value相同)。 |
set | HashSet | 否 | 当前调用forEach方法的实例对象。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The forEach method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("sparrow");
hashSet.add("squirrel");
hashSet.forEach((value, key) = > {
console.log("value:" + value, "key:" + key);
});
entries
entries(): IterableIterator<[T, T]>
返回包含此映射中包含的键值对的新迭代器对象。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
IterableIterator<[T, T]> | 返回一个迭代器。 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The entries method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let iter = hashSet.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]
HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
Symbol.iterator: IterableIterator
返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
IterableIterator | 返回一个迭代器 |
错误码:
以下错误码的详细介绍请参见[语言基础类库错误码]。
错误码ID | 错误信息 |
---|---|
10200011 | The Symbol.iterator method cannot be bound. |
示例:
let hashSet = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
// 使用方法一:
for (let item of hashSet) {
console.log("value: " + item);
}
// 使用方法二:
let iter = hashSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log("value: " + temp);
temp = iter.next().value;
}
审核编辑 黄宇
-
鸿蒙
+关注
关注
57文章
2398浏览量
43133 -
OpenHarmony
+关注
关注
25文章
3753浏览量
16718
发布评论请先 登录
相关推荐
OpenHarmony语言基础类库【@ohos.util.ArrayList (线性容器ArrayList)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.ArrayList (<b class='flag-5'>线性</b><b class='flag-5'>容器</b>ArrayList)】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
OpenHarmony语言基础类库【@ohos.util.HashMap (非线性容器HashMap)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.HashMap (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>HashMap)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
OpenHarmony语言基础类库【@ohos.util.LightWeightMap (非线性容器LightWeightMap)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.LightWeightMap (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>LightWeightMap)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
OpenHarmony语言基础类库【@ohos.util.LightWeightSet (非线性容器LightWeightSet)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.LightWeightSet (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>LightWeightSet)】](https://file1.elecfans.com/web2/M00/E6/F0/wKgaomZF27WAC94sAABDudJkBSs062.png)
OpenHarmony语言基础类库【@ohos.util.PlainArray (非线性容器PlainArray)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.PlainArray (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>PlainArray)】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
OpenHarmony语言基础类库【@ohos.util.Vector (线性容器Vector)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.Vector (<b class='flag-5'>线性</b><b class='flag-5'>容器</b>Vector)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
HarmonyOS方舟开发框架容器类API的介绍与使用
OpenHarmony 3.1 Beta版本关键特性解析——ArkUI容器类API介绍
HarmonyOS非线性容器特性及使用场景
HarmonyOS语言基础类库开发指南上线啦!
JDK中java.util.HashSet 类的介绍
![JDK中java.<b class='flag-5'>util.HashSet</b> <b class='flag-5'>类</b>的介绍](https://file1.elecfans.com/web2/M00/A7/5C/wKgaomUjaBCALSR7AAB1hncWMPg648.jpg)
java的util包下有哪些类
HarmonyOS 非线性容器特性及使用场景
OpenHarmony语言基础类库【@ohos.util.TreeMap (非线性容器TreeMap)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.TreeMap (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>TreeMap)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
OpenHarmony语言基础类库【@ohos.util.TreeSet (非线性容器TreeSet)】
![<b class='flag-5'>OpenHarmony</b><b class='flag-5'>语言</b>基础<b class='flag-5'>类</b><b class='flag-5'>库</b>【@<b class='flag-5'>ohos.util</b>.TreeSet (<b class='flag-5'>非线性</b><b class='flag-5'>容器</b>TreeSet)】](https://file1.elecfans.com/web2/M00/C5/D1/wKgZomYChGOAUaiiAADe1d8SeRY102.jpg)
评论