HarmonyOS 非线性容器HashSet 常用的几个方法
·
HashSet基于HashMap实现。在HashSet中,只对value对象进行处理。
HashSet和TreeSet相比,HashSet中的数据无序存放,即存放元素的顺序和取出的顺序不一致,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不建议存放null值,可能会对排序结果产生影响。
推荐使用场景: 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。
HashSet
1. constructor
2. isEmpty
3. has
4. add
5. remove
6. clear
7. values
8. forEach
9. entries
10. [Symbol.iterator]
导入模块
import { HashSet } from '@kit.ArkTS';
HashSet
属性
| 名称 | 类型 | 可读 | 可写 | 说明 |
|---|---|---|---|---|
| length | number | 是 | 否 | HashSet的元素个数。 |
使用方式:
let hashSet: HashSet<number> = new HashSet();
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
let res = hashSet.length;
1. constructor
constructor()
HashSet的构造函数。
使用方式:
let hashSet: HashSet<number> = new HashSet();
2. isEmpty
isEmpty(): boolean
判断该HashSet是否为空。
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 为空返回true,不为空返回false。 |
使用方式:
const hashSet: HashSet<number> = new HashSet();
let result = hashSet.isEmpty();
3. has
has(value: T): boolean
判断此HashSet中是否含有该指定元素。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | T | 是 | 指定元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 包含指定元素返回true,否则返回false。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
let result = hashSet.has("squirrel");
4. add
add(value: T): boolean
向HashSet中添加数据。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | T | 是 | 添加成员数据。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 成功增加元素返回true,否则返回false。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
let result = hashSet.add("squirrel");
5. remove
remove(value: T): boolean
删除指定的元素。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | T | 是 | 指定删除的元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 成功删除指定元素返回true,否则返回false。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let result = hashSet.remove("sparrow");
6. clear
clear(): void
清除HashSet中的所有元素,并把length置为0。
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
hashSet.clear();
7. values
values(): IterableIterator<T>
返回包含此映射中包含的键值的新迭代器对象。
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<T> | 返回一个迭代器。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let iter = hashSet.values();
let temp = iter.next();
while(!temp.done) {
console.log("value:" + temp.value);
temp = iter.next();
}
8. forEach
forEach(callbackFn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void
通过回调函数来遍历实例对象上的元素以及元素对应的下标。
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | T | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 |
| key | T | 否 | 当前遍历到的元素键值对的键(和value相同),默认值为首个键值对的键。 |
| set | HashSet<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("sparrow");
hashSet.add("squirrel");
hashSet.forEach((value?: string, key?: string): void => {
console.log("value:" + value, "key:" + key);
});
// 不建议在forEach中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let hashSet : HashSet<string> = new HashSet();
for(let i = 0;i < 10; i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0;i < 10; i++) {
hashSet.remove("sparrow" + i);
}
9. entries
entries(): IterableIterator<[T, T]>
返回包含此映射中包含的键值对的新迭代器对象。
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<[T, T]> | 返回一个迭代器。 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
let iter = hashSet.entries();
let temp: IteratorResult<[string, string]> = iter.next();
while(!temp.done) {
console.log("key:" + temp.value[0]);
console.log("value:" + temp.value[1]);
temp = iter.next();
}
// 不建议在entries中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let hashSet : HashSet<string> = new HashSet();
for(let i = 0;i < 10; i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0;i < 10; i++) {
hashSet.remove("sparrow" + i);
}
10. [Symbol.iterator]
[Symbol.iterator](): IterableIterator<T>
返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<T> | 返回一个迭代器 |
使用方式:
let hashSet: HashSet<string> = new HashSet();
hashSet.add("squirrel");
hashSet.add("sparrow");
// 使用方法一:
let val: Array<string> = Array.from(hashSet.values())
for (let item of val) {
console.log("value: " + item);
}
// 使用方法二:
let iter = hashSet[Symbol.iterator]();
let temp: IteratorResult<string> = iter.next();
while(!temp.done) {
console.log("value: " + temp.value);
temp = iter.next();
}
// 不建议在Symbol.iterator中使用set、remove方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
let hashSet : HashSet<string> = new HashSet();
for(let i = 0;i < 10;i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0;i < 10;i++) {
hashSet.remove("sparrow" + i);
}
如需要其他方法 请参考官方文档
制作不易 点个关注再走吧。°(°¯᷄◠¯᷅°)°。
更多推荐



所有评论(0)