跳到主要内容

@ohos.util.HashSet (非线性容器HashSet)

HashSet基于HashMap实现。在HashSet中,仅处理value对象。

HashSet和TreeSet相比,HashSet中的数据按Hash值排序,因此元素的插入顺序与遍历时的顺序可能不一致,而TreeSet则是按照元素的自然排序或者自定义比较器进行有序存储。它们集合中的元素都不允许重复,HashSet允许插入null值,TreeSet不建议插入null值,会影响排序结果。

推荐使用场景: 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。

文档中使用了泛型,涉及以下泛型标记符:

  • T:Type,类

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

容器类使用静态语言实现,限制了存储位置和属性,不支持自定义属性和方法。

导入模块

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

HashSet

属性

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

名称类型只读可选说明
lengthnumberHashSet的元素个数。

示例:

let hashSet = new HashSet<number>();
hashSet.add(1);
hashSet.add(2);
hashSet.add(3);
hashSet.add(4);
hashSet.add(5);
let res = hashSet.length;
console.info("length:", res); // length: 5

constructor

constructor()

HashSet的构造函数。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200012The HashSet's constructor cannot be directly invoked.

示例:

let hashSet = new HashSet<number>();

isEmpty

isEmpty(): boolean

判断HashSet是否为空。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The isEmpty method cannot be bound.

示例:

const hashSet = new HashSet<number>();
let result = hashSet.isEmpty();
console.info("result:", result); // result: true

has

has(value: T): boolean

判断HashSet是否包含指定元素。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID错误信息
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200011The has method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
let result = hashSet.has("squirrel");
console.info("result:", result); // result: true

add

add(value: T): boolean

向HashSet添加元素。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
valueT添加成员数据。

返回值:

类型说明
boolean成功添加元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID错误信息
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200011The add method cannot be bound.

示例:

let hashSet = new HashSet<string>();
let result = hashSet.add("squirrel");
console.info("result:", result); // result: true

remove

remove(value: T): boolean

从HashSet中删除指定的元素。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
valueT指定删除的元素。

返回值:

类型说明
boolean成功删除指定元素返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID错误信息
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.
10200011The remove method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
hashSet.add("sparrow");
let result = hashSet.remove("sparrow");
console.info("result:", result); // result: true

clear

clear(): void

清除HashSet中的所有元素,并将length置为0。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The clear method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
hashSet.add("sparrow");
hashSet.clear();
let result = hashSet.isEmpty();
console.info("result:", result); // result: true

values

values(): IterableIterator<T>

返回包含此映射中所有键值的新迭代器对象。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
IterableIterator<T>返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The values method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
hashSet.add("sparrow");
let values = hashSet.values();
for (let value of values) {
console.info("value:", value);
}
// value: squirrel
// value: sparrow

forEach

forEach(callbackFn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void

在遍历过程中对每个元素调用一次回调函数。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

参数:

参数名类型必填说明
callbackFnfunction回调函数。
thisArgObjectcallbackFn被调用时用作this值,默认值为当前实例对象。

callbackFn的参数说明:

参数名类型必填说明
valueT当前遍历到的元素键值对的值。
keyT当前遍历到的元素键值对的键(和value相同)。
setHashSet<T>当前调用forEach方法的实例对象,默认值为当前实例对象。

错误码:

以下错误码的详细介绍请参见通用错误码语言基础类库错误码

错误码ID错误信息
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
10200011The forEach method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("sparrow");
hashSet.add("squirrel");
hashSet.forEach((value: string, key: string): void => {
console.info("value:" + value, "key:" + key);
});
// value:squirrel key:squirrel
// value:sparrow key:sparrow
// 不建议在forEach中使用add、remove方法,因其可能导致迭代过程中的状态异常,建议使用for循环来进行安全的插入与删除操作。
let hashSet = new HashSet<string>();
for(let i = 0; i < 10; i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0; i < 10; i++) {
hashSet.remove("sparrow" + i);
}

entries

entries(): IterableIterator<[T, T]>

返回包含此映射中所有键值对的新迭代器对象。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
IterableIterator<[T, T]>返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The entries method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
hashSet.add("sparrow");
let iter = hashSet.entries();
let temp: IteratorResult<[string, string]> = iter.next();
while(!temp.done) {
console.info("key:" + temp.value[0]);
console.info("value:" + temp.value[1]);
temp = iter.next();
}
// key:squirrel
// value:squirrel
// key:sparrow
// value:sparrow
// 不建议在entries中使用set、remove方法,因其可能导致迭代过程中的状态异常,建议使用for循环来进行安全的插入与删除操作。
let hashSet = new HashSet<string>();
for(let i = 0; i < 10; i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0; i < 10; i++) {
hashSet.remove("sparrow" + i);
}

[Symbol.iterator]

Symbol.iterator: IterableIterator<T>

返回一个迭代器,迭代器的每一项都是一个JavaScript对象。

元服务API: 从API version 12开始,该接口支持在元服务中使用。

系统能力: SystemCapability.Utils.Lang

返回值:

类型说明
IterableIterator<T>返回一个迭代器。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID错误信息
10200011The Symbol.iterator method cannot be bound.

示例:

let hashSet = new HashSet<string>();
hashSet.add("squirrel");
hashSet.add("sparrow");

// 使用方法一:
for (let item of hashSet) {
console.info("value: " + item);
}
// value: squirrel
// value: sparrow

// 使用方法二:
let iter = hashSet[Symbol.iterator]();
let temp: IteratorResult<string> = iter.next();
while(!temp.done) {
console.info("value: " + temp.value);
temp = iter.next();
}
// value: squirrel
// value: sparrow
// 不建议在Symbol.iterator中使用set、remove方法,因其可能导致迭代过程中的状态异常,建议使用for循环来进行安全的插入与删除操作。
let hashSet = new HashSet<string>();
for(let i = 0;i < 10;i++) {
hashSet.add("sparrow" + i);
}
for(let i = 0;i < 10;i++) {
hashSet.remove("sparrow" + i);
}