collections.Set
一种非线性数据结构。
文档中存在泛型的使用,涉及以下泛型标记符:
- T:Type,支持Sendable的数据类型。
属性
元服务API:从API version 12 开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
名称 | 类型 | 只读 | 可选 | 说明 |
---|---|---|---|---|
size | number | 是 | 否 | Set的元素个数。 |
constructor
constructor(values?: readonly T[] | null)
构造函数,用于创建ArkTS Set对象。
元服务API:从API version 12 开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
values | T[] | null | 否 | 数组或其它可迭代对象。默认值为null,创建一个空Set对象。 |
错误码:
错误码ID | 错误信息 |
---|---|
10200012 | The ArkTS Set's constructor cannot be directly invoked. |
示例:
// 正例1:
const mySet = new collections.Set<number>();
// 正例2:
const mySet = new collections.Set<number>([1, 2, 3, 4, 5]);
// 反例:
@Sendable
class SharedClass {
constructor() {
}
}
let sObj = new SharedClass();
const mySet1: collections.Set<number|SharedClass> = new collections.Set<number|SharedClass>([1, sObj]);
// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types)
let obj = new Object();
const mySet2: collections.Set<number|SharedClass> = new collections.Set<number|Object>([1, obj]);
entries
entries(): IterableIterator<[T, T]>
返回一个Set迭代器对象。
元服务API:从API version 12 开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils