1# Class (Set) 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @lijiamin2025--> 5<!--Designer: @weng-changcheng--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8> **说明:** 9> 10> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 11> 12> 此模块仅支持在ArkTS文件(文件后缀为.ets)中导入使用。 13 14一种非线性数据结构。 15 16文档中存在泛型的使用,涉及以下泛型标记符: 17 18- T:Type,支持[Sendable支持的数据类型](../../arkts-utils/arkts-sendable.md#sendable支持的数据类型)。 19 20**装饰器类型:**\@Sendable 21 22## 导入模块 23 24```ts 25import { collections } from '@kit.ArkTS'; 26``` 27 28## 属性 29 30**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 31 32**系统能力:** SystemCapability.Utils.Lang 33 34| 名称 | 类型 | 只读 | 可选 | 说明 | 35| ---- | ------ | ---- | ---- | --------------- | 36| size | number | 是 | 否 | Set的元素个数。 | 37 38## constructor 39 40constructor(values?: readonly T[] | null) 41 42构造函数,用于创建ArkTS Set对象。 43 44**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 45 46**系统能力:** SystemCapability.Utils.Lang 47 48**参数:** 49 50| 参数名 | 类型 | 必填 | 说明 | 51| ------ | ---- | ---- | --------------------------------------------------------- | 52| values | readonly T[] \| null | 否 | 数组或其它可迭代对象。默认值为null,创建一个空Set对象。 | 53 54**错误码:** 55 56以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 57 58| 错误码ID | 错误信息 | 59| -------- | ------------------------------------------------------- | 60| 401 | Parameter error. | 61| 10200012 | The ArkTS Set's constructor cannot be directly invoked. | 62 63**示例:** 64 65```ts 66// 正例1: 67const mySet = new collections.Set<number>(); 68``` 69 70```ts 71// 正例2: 72const mySet = new collections.Set<number>([1, 2, 3, 4, 5]); 73``` 74 75<!--code_no_check--> 76```ts 77// 反例: 78@Sendable 79class SharedClass { 80 constructor() { 81 } 82} 83 84let sObj = new SharedClass(); 85const mySet1: collections.Set<number|SharedClass> = new collections.Set<number|SharedClass>([1, sObj]); 86// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 87let obj = new Object(); 88const mySet2: collections.Set<number|SharedClass> = new collections.Set<number|Object>([1, obj]); 89``` 90 91## entries 92entries(): IterableIterator<[T, T]> 93 94返回一个Set迭代器对象。 95 96**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 97 98**系统能力:** SystemCapability.Utils.Lang 99 100**返回值:** 101 102| 类型 | 说明 | 103| ------------------------------ | ----------------------- | 104| IterableIterator<[T, T]> | 返回一个Set迭代器对象。 | 105 106**错误码:** 107 108以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 109 110| 错误码ID | 错误信息 | 111| -------- | ----------------------------------------------------- | 112| 10200011 | The entries method cannot be bound with non-sendable. | 113| 10200201 | Concurrent modification error. | 114 115**示例:** 116 117```ts 118const mySet = new collections.Set<number>([0, 1, 2, 3]); 119 120const iterator = mySet.entries(); 121// Expected output: [0, 0] 122console.info(iterator.next().value); 123// Expected output: [1, 1] 124console.info(iterator.next().value); 125``` 126 127## keys 128keys(): IterableIterator\<T> 129 130返回一个Set迭代器对象,该对象包含了此Set中每个元素的值。 131 132**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 133 134**系统能力:** SystemCapability.Utils.Lang 135 136**返回值:** 137 138| 类型 | 说明 | 139| ------------------------- | ----------------------- | 140| IterableIterator<T> | 返回一个Set迭代器对象。 | 141 142**错误码:** 143 144以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 145 146| 错误码ID | 错误信息 | 147| -------- | -------------------------------------------------- | 148| 10200011 | The keys method cannot be bound with non-sendable. | 149| 10200201 | Concurrent modification error. | 150 151**示例:** 152 153```ts 154const mySet = new collections.Set<number>([0, 1, 2, 3]); 155 156const iterator = mySet.keys(); 157// Expected output: 0 158console.info(iterator.next().value); 159// Expected output: 1 160console.info(iterator.next().value); 161``` 162 163## values 164values(): IterableIterator\<T> 165 166返回一个Set迭代器对象,该对象包含了此Set中每个元素的值。 167 168**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 169 170**系统能力:** SystemCapability.Utils.Lang 171 172**返回值:** 173 174| 类型 | 说明 | 175| ------------------------- | ----------------------- | 176| IterableIterator<T> | 返回一个Set迭代器对象。 | 177 178**错误码:** 179 180以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 181 182| 错误码ID | 错误信息 | 183| -------- | ---------------------------------------------------- | 184| 10200011 | The values method cannot be bound with non-sendable. | 185| 10200201 | Concurrent modification error. | 186 187**示例:** 188 189```ts 190// 例1: 191const mySet = new collections.Set<number>([0, 1, 2, 3]); 192 193const iterator = mySet.values(); 194// Expected output: 0 195console.info(iterator.next().value); 196// Expected output: 1 197console.info(iterator.next().value); 198``` 199 200```ts 201// 例2: 202const mySet = new collections.Set<number>([0, 1, 2, 3]); 203 204const valueIter = mySet.values(); 205for (let value of valueIter) { 206 if (value % 2 == 0) { 207 mySet.delete(value); 208 } 209} 210 211// Expected output: 2 212console.info("size:" + mySet.size); 213``` 214 215## clear 216clear(): void 217 218删除该Set中的所有元素。 219 220**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 221 222**系统能力:** SystemCapability.Utils.Lang 223 224**错误码:** 225 226以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 227 228| 错误码ID | 错误信息 | 229| -------- | --------------------------------------------------- | 230| 10200011 | The clear method cannot be bound with non-sendable. | 231| 10200201 | Concurrent modification error. | 232 233**示例:** 234 235```ts 236const mySet = new collections.Set<number>([0, 1]); 237// Expected output: 2 238console.info("size:" + mySet.size); 239mySet.clear(); 240// Expected output: 0 241console.info("size:" + mySet.size); 242``` 243 244## delete 245delete(value: T): boolean 246 247删除该Set中指定元素。 248 249**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 250 251**系统能力:** SystemCapability.Utils.Lang 252 253**参数:** 254 255| 参数名 | 类型 | 必填 | 说明 | 256| ------ | ---- | ---- | ---------------- | 257| value | T | 是 | 待删除元素的值。 | 258 259**返回值:** 260 261| 类型 | 说明 | 262| ------- | --------------------------------- | 263| boolean | 成功删除返回true,否则返回false。 | 264 265**错误码:** 266 267以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 268 269| 错误码ID | 错误信息 | 270| -------- | ---------------------------------------------------- | 271| 10200011 | The delete method cannot be bound with non-sendable. | 272| 10200201 | Concurrent modification error. | 273 274 275**示例:** 276 277```ts 278const mySet = new collections.Set<string>(["hello", "world"]); 279// Expected result: true 280console.info("result:" + mySet.delete("hello")); 281// Expected result: false 282console.info("result:" + mySet.has("hello")); 283// Expected result: false 284console.info("result:" + mySet.delete("hello")); 285``` 286 287## forEach 288forEach(callbackFn: (value: T, value2: T, set: Set\<T>) => void): void 289 290按插入顺序对该Set中的每个键/值对执行一次回调函数。 291 292**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 293 294**系统能力:** SystemCapability.Utils.Lang 295 296**参数:** 297 298| 参数名 | 类型 | 必填 | 说明 | 299| ---------- | -------------------------------------------- | ---- | ---------- | 300| callbackFn | (value: T, value2: T, set: Set\<T>) => void | 是 | 回调函数。 | 301 302callbackFn的参数说明: 303| 参数名 | 类型 | 必填 | 说明 | 304| ------ | ------------ | ---- | ---------------------------- | 305| value | T | 否 | 当前遍历到的元素键值对的值。 | 306| value2 | T | 否 | 当前遍历到的元素键值对的键。 | 307| set | Set<T> | 否 | 当前set实例对象。 | 308 309**错误码:** 310 311以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 312 313| 错误码ID | 错误信息 | 314| -------- | ----------------------------------------------------- | 315| 401 | Parameter error. | 316| 10200011 | The forEach method cannot be bound with non-sendable. | 317| 10200201 | Concurrent modification error. | 318 319**示例:** 320 321```ts 322// 正例: 323new collections.Set<string>(['foo', 'bar', 'baz']).forEach((value1, value2, set) => { 324 console.info(`s[${value1}] = ${value2}`); 325}); 326``` 327 328<!--code_no_check--> 329```ts 330// 反例: 331new collections.Set<string>(['foo', 'bar', 'baz']).forEach((value1, value2, set) => { 332 // Throw exception `Concurrent modification error.` 333 set.delete(value1); 334}); 335``` 336 337## has 338has(value: T): boolean 339 340判断该Set中是否存在指定元素。 341 342**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 343 344**系统能力:** SystemCapability.Utils.Lang 345 346**参数:** 347 348| 参数名 | 类型 | 必填 | 说明 | 349| ------ | ---- | ---- | ---------------- | 350| value | T | 是 | 待查找元素的值。 | 351 352**返回值:** 353 354| 类型 | 说明 | 355| ------- | --------------------------------------------- | 356| boolean | 如果存在指定元素,则返回true;否则返回false。 | 357 358**错误码:** 359 360以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 361 362| 错误码ID | 错误信息 | 363| -------- | ------------------------------------------------- | 364| 401 | Parameter error. | 365| 10200011 | The has method cannot be bound with non-sendable. | 366| 10200201 | Concurrent modification error. | 367 368**示例:** 369 370```ts 371const mySet = new collections.Set<string>(["hello", "world"]); 372// Expected output: true 373console.info("result:" + mySet.has("hello")); 374// Expected output: true 375console.info("result:" + mySet.has("world")); 376``` 377 378## add 379add(value: T): Set\<T> 380 381如果没有相同元素,则在该Set中插入一个新元素。 382 383**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 384 385**系统能力:** SystemCapability.Utils.Lang 386 387**参数:** 388 389| 参数名 | 类型 | 必填 | 说明 | 390| ------ | ---- | ---- | ---------------- | 391| value | T | 是 | 待插入元素的值。 | 392 393**返回值:** 394 395| 类型 | 说明 | 396| ------------ | --------- | 397| Set<T> | Set对象。 | 398 399**错误码:** 400 401以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 402 403| 错误码ID | 错误信息 | 404| -------- | ------------------------------------------------- | 405| 10200011 | The add method cannot be bound with non-sendable. | 406| 10200201 | Concurrent modification error. | 407 408**示例:** 409 410```ts 411// 正例: 412const mySet: collections.Set<string> = new collections.Set<string>(); 413mySet.add("foo"); 414``` 415 416<!--code_no_check--> 417```ts 418// 反例: 419let obj = new Object(); 420const mySet: collections.Set<Object> = new collections.Set<Object>(); 421// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 422mySet.add(obj); 423``` 424 425## [Symbol.iterator] 426 427[Symbol.iterator]\(): IterableIterator<T> 428 429返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 430 431> **说明:** 432> 433> 本接口不支持在.ets文件中使用。 434 435**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 436 437**系统能力:** SystemCapability.Utils.Lang 438 439**返回值:** 440 441| 类型 | 说明 | 442| -------- | -------- | 443| IterableIterator<T> | 返回一个迭代器。 | 444 445**错误码:** 446 447以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 448 449| 错误码ID | 错误信息 | 450| -------- | -------- | 451| 10200011 | The Symbol.iterator method cannot be bound. | 452 453**示例:** 454 455```ts 456let set = new collections.Set<number>([1, 2, 3, 4, 5]); 457 458let val: Array<number> = Array.from(set.values()); 459for (let item of val) { 460 console.info("value: " + item); 461} 462``` 463