1# @ohos.util.LightWeightSet (非线性容器LightWeightSet)
2
3LightWeightSet可用于存储一系列值的集合,存储元素中value值唯一。
4
5LightWeightSet依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。
6
7集合中value值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的value值。
8
9LightWeightSet和[HashSet](js-apis-hashset.md)都是用来存储键值的集合,LightWeightSet的占用内存更小。
10
11**推荐使用场景:** 当需要存取某个集合或是对某个集合去重时,推荐使用占用内存更小的LightWeightSet。
12
13文档中存在泛型的使用,涉及以下泛型标记符:<br>
14- T:Type,类
15
16> **说明:**
17>
18> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
19
20
21## 导入模块
22
23```ts
24import { LightWeightSet } from '@kit.ArkTS';
25```
26
27## LightWeightSet
28
29### 属性
30
31**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
32
33**系统能力:** SystemCapability.Utils.Lang
34
35| 名称 | 类型 | 可读 | 可写 | 说明 |
36| -------- | -------- | -------- | -------- | -------- |
37| length | number | 是 | 否 | LightWeightSet的元素个数。 |
38
39
40### constructor
41
42constructor()
43
44LightWeightSet的构造函数。
45
46**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
47
48**系统能力:** SystemCapability.Utils.Lang
49
50**错误码:**
51
52以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
53
54| 错误码ID | 错误信息 |
55| -------- | -------- |
56| 10200012 | The LightWeightSet's constructor cannot be directly invoked. |
57
58**示例:**
59
60```ts
61let lightWeightSet: LightWeightSet<number | string> = new LightWeightSet();
62```
63
64
65### isEmpty
66
67isEmpty(): boolean
68
69判断该容器是否为空。
70
71**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
72
73**系统能力:** SystemCapability.Utils.Lang
74
75**返回值:**
76
77| 类型 | 说明 |
78| -------- | -------- |
79| boolean | 为空返回true,不为空返回false。 |
80
81**错误码:**
82
83以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
84
85| 错误码ID | 错误信息 |
86| -------- | -------- |
87| 10200011 | The isEmpty method cannot be bound. |
88
89**示例:**
90
91```ts
92const lightWeightSet: LightWeightSet<number> = new LightWeightSet();
93let result = lightWeightSet.isEmpty();
94```
95
96### add
97
98add(obj: T): boolean
99
100向此容器中添加数据。
101
102**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
103
104**系统能力:** SystemCapability.Utils.Lang
105
106**参数:**
107
108| 参数名 | 类型 | 必填 | 说明 |
109| -------- | -------- | -------- | -------- |
110| obj | T | 是 | 添加的成员数据。 |
111
112**返回值:**
113
114| 类型 | 说明 |
115| -------- | -------- |
116| boolean | 成功添加元素返回true,否则返回false。 |
117
118**错误码:**
119
120以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
121
122| 错误码ID | 错误信息 |
123| -------- | -------- |
124| 10200011 | The add method cannot be bound. |
125
126**示例:**
127
128```ts
129let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
130let result = lightWeightSet.add("squirrel");
131```
132
133
134### addAll
135
136addAll(set: LightWeightSet&lt;T&gt;): boolean
137
138将另一个容器中的所有元素组添加到当前容器中。
139
140**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
141
142**系统能力:** SystemCapability.Utils.Lang
143
144**参数:**
145
146| 参数名 | 类型 | 必填 | 说明 |
147| -------- | -------- | -------- | -------- |
148| set | LightWeightSet&lt;T&gt; | 是 | 提供添加元素的lightWeightSet。 |
149
150**返回值:**
151
152| 类型 | 说明 |
153| -------- | -------- |
154| boolean | 成功添加元素返回true,否则返回false。 |
155
156**错误码:**
157
158以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
159
160| 错误码ID | 错误信息 |
161| -------- | -------- |
162| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
163| 10200011 | The addAll method cannot be bound. |
164
165**示例:**
166
167```ts
168let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
169lightWeightSet.add("squirrel");
170lightWeightSet.add("sparrow");
171let set: LightWeightSet<string> = new LightWeightSet();
172set.add("gull");
173let result = lightWeightSet.addAll(set);
174```
175
176
177### hasAll
178
179hasAll(set: LightWeightSet&lt;T&gt;): boolean
180
181判断此容器中是否含有该指定set中的所有元素。
182
183**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
184
185**系统能力:** SystemCapability.Utils.Lang
186
187**参数:**
188
189| 参数名 | 类型 | 必填 | 说明 |
190| -------- | -------- | -------- | -------- |
191| set | LightWeightSet&lt;T&gt; | 是 | 比较对象。 |
192
193**返回值:**
194
195| 类型 | 说明 |
196| -------- | -------- |
197| boolean | 包含所有元素返回true,否则返回false。 |
198
199**错误码:**
200
201以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
202
203| 错误码ID | 错误信息 |
204| -------- | -------- |
205| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
206| 10200011 | The hasAll method cannot be bound. |
207
208**示例:**
209
210```ts
211let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
212lightWeightSet.add("squirrel");
213lightWeightSet.add("sparrow");
214let set: LightWeightSet<string> = new LightWeightSet();
215set.add("sparrow");
216let result = lightWeightSet.hasAll(set);
217```
218
219
220### has
221
222has(key: T): boolean
223
224判断此容器中是否含有该指定key。
225
226**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
227
228**系统能力:** SystemCapability.Utils.Lang
229
230**参数:**
231
232| 参数名 | 类型 | 必填 | 说明 |
233| -------- | -------- | -------- | -------- |
234| key | T | 是 | 指定key |
235
236**返回值:**
237
238| 类型 | 说明 |
239| -------- | -------- |
240| boolean | 包含指定key返回true,否则返回false。 |
241
242**错误码:**
243
244以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
245
246| 错误码ID | 错误信息 |
247| -------- | -------- |
248| 10200011 | The has method cannot be bound. |
249
250**示例:**
251
252```ts
253let lightWeightSet: LightWeightSet<number> = new LightWeightSet();
254lightWeightSet.add(123);
255let result = lightWeightSet.has(123);
256```
257
258
259### increaseCapacityTo
260
261increaseCapacityTo(minimumCapacity: number): void
262
263将当前LightWeightSet扩容至可以容纳指定数量元素。如果传入的容量值大于或等于当前LightWeightSet中的元素个数,将容量变更为新容量,小于则不会变更。
264
265**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
266
267**系统能力:** SystemCapability.Utils.Lang
268
269**参数:**
270
271| 参数名 | 类型 | 必填 | 说明 |
272| -------- | -------- | -------- | -------- |
273| minimumCapacity | number | 是 | 需要容纳的元素数量。 |
274
275**错误码:**
276
277以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
278
279| 错误码ID | 错误信息 |
280| -------- | -------- |
281| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
282| 10200001 | The value of minimumCapacity is out of range. |
283| 10200011 | The increaseCapacityTo method cannot be bound. |
284
285**示例:**
286
287```ts
288let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
289lightWeightSet.increaseCapacityTo(10);
290```
291
292
293### getIndexOf
294
295getIndexOf(key: T): number
296
297获取指定key所对应的下标。
298
299**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
300
301**系统能力:** SystemCapability.Utils.Lang
302
303**参数:**
304
305| 参数名 | 类型 | 必填 | 说明 |
306| -------- | -------- | -------- | -------- |
307| key | T | 是 | 查找的指定key。 |
308
309**返回值:**
310
311| 类型 | 说明 |
312| -------- | -------- |
313| number | 在lightWeightSet中指定数据的下标。若lightWeightSet中没有要查找的元素,则返回一个负值。表示目标哈希值应该插入的位置,插入位置是从1开始计数的,负号表示这是一个插入位置而不是索引。 |
314
315**错误码:**
316
317以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
318
319| 错误码ID | 错误信息 |
320| -------- | -------- |
321| 10200011 | The getIndexOf method cannot be bound. |
322
323**示例:**
324
325```ts
326let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
327lightWeightSet.add("squirrel");
328lightWeightSet.add("sparrow");
329let result = lightWeightSet.getIndexOf("sparrow");
330```
331
332
333### remove
334
335remove(key: T): T
336
337删除并返回指定key对应的元素。
338
339**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
340
341**系统能力:** SystemCapability.Utils.Lang
342
343**参数:**
344
345| 参数名 | 类型 | 必填 | 说明 |
346| -------- | -------- | -------- | -------- |
347| key | T | 是 | 指定key。 |
348
349**返回值:**
350
351| 类型 | 说明 |
352| -------- | -------- |
353| T | 返回删除元素的值。 |
354
355**错误码:**
356
357以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
358
359| 错误码ID | 错误信息 |
360| -------- | -------- |
361| 10200011 | The remove method cannot be bound. |
362
363**示例:**
364
365```ts
366let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
367lightWeightSet.add("squirrel");
368lightWeightSet.add("sparrow");
369let result = lightWeightSet.remove("sparrow");
370```
371
372
373### removeAt
374
375removeAt(index: number): boolean
376
377删除指定下标所对应的元素。
378
379**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
380
381**系统能力:** SystemCapability.Utils.Lang
382
383**参数:**
384
385| 参数名 | 类型 | 必填 | 说明 |
386| -------- | -------- | -------- | -------- |
387| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 |
388
389**返回值:**
390
391| 类型 | 说明 |
392| -------- | -------- |
393| boolean | 确认是否成功删除元素,成功删除元素返回true,否则返回false。 |
394
395**错误码:**
396
397以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
398
399| 错误码ID | 错误信息 |
400| -------- | -------- |
401| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
402| 10200011 | The removeAt method cannot be bound. |
403
404**示例:**
405
406```ts
407let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
408lightWeightSet.add("squirrel");
409lightWeightSet.add("sparrow");
410let result = lightWeightSet.removeAt(1);
411```
412
413
414### getValueAt
415
416getValueAt(index: number): T
417
418获取此容器中指定下标对应的元素。
419
420**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
421
422**系统能力:** SystemCapability.Utils.Lang
423
424**参数:**
425
426| 参数名 | 类型 | 必填 | 说明 |
427| -------- | -------- | -------- | -------- |
428| index | number | 是 | 指定下标。需要小于等于int32_max即2147483647。 |
429
430**返回值:**
431
432| 类型 | 说明 |
433| -------- | -------- |
434| T | 返回指定下标对应的元素。 |
435
436**错误码:**
437
438以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
439
440| 错误码ID | 错误信息 |
441| -------- | -------- |
442| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
443| 10200011 | The getValueAt method cannot be bound. |
444
445**参数:**
446
447```ts
448let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
449lightWeightSet.add("squirrel");
450lightWeightSet.add("sparrow");
451let result = lightWeightSet.getValueAt(1);
452```
453
454
455### clear
456
457clear(): void
458
459清除容器中的所有元素,并把length置为0。
460
461**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
462
463**系统能力:** SystemCapability.Utils.Lang
464
465**错误码:**
466
467以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
468
469| 错误码ID | 错误信息 |
470| -------- | -------- |
471| 10200011 | The clear method cannot be bound. |
472
473**示例:**
474
475```ts
476let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
477lightWeightSet.add("squirrel");
478lightWeightSet.add("sparrow");
479lightWeightSet.clear();
480```
481
482
483### toString
484
485toString(): String
486
487获取包含容器中所有键和值的字符串。
488
489**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
490
491**系统能力:** SystemCapability.Utils.Lang
492
493**返回值:**
494
495| 类型 | 说明 |
496| -------- | -------- |
497| String | 返回对应字符串。 |
498
499**示例:**
500
501```ts
502let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
503lightWeightSet.add("squirrel");
504lightWeightSet.add("sparrow");
505let result = lightWeightSet.toString();
506```
507
508
509### toArray
510
511toArray(): Array&lt;T&gt;
512
513获取包含此容器中所有对象的数组。
514
515**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
516
517**系统能力:** SystemCapability.Utils.Lang
518
519**返回值:**
520
521| 类型 | 说明 |
522| -------- | -------- |
523| Array&lt;T&gt; | 返回对应数组。 |
524
525**错误码:**
526
527以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
528
529| 错误码ID | 错误信息 |
530| -------- | -------- |
531| 10200011 | The toArray method cannot be bound. |
532
533**示例:**
534
535```ts
536let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
537lightWeightSet.add("squirrel");
538lightWeightSet.add("sparrow");
539let result = lightWeightSet.toArray();
540```
541
542
543### values
544
545values(): IterableIterator&lt;T&gt;
546
547返回包含此映射中包含的键值的新迭代器对象。
548
549**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
550
551**系统能力:** SystemCapability.Utils.Lang
552
553**返回值:**
554
555| 类型 | 说明 |
556| -------- | -------- |
557| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
558
559**错误码:**
560
561以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
562
563| 错误码ID | 错误信息 |
564| -------- | -------- |
565| 10200011 | The values method cannot be bound. |
566
567**示例:**
568
569```ts
570let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
571lightWeightSet.add("squirrel");
572lightWeightSet.add("sparrow");
573let iter = lightWeightSet.values();
574let index = 0;
575while(index < lightWeightSet.length) {
576  console.log(JSON.stringify(iter.next().value));
577  index++;
578}
579```
580
581
582### forEach
583
584forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
585
586通过回调函数来遍历LightWeightSet实例对象上的元素以及元素对应的下标。
587
588**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
589
590**系统能力:** SystemCapability.Utils.Lang
591
592**参数:**
593
594| 参数名 | 类型 | 必填 | 说明 |
595| -------- | -------- | -------- | -------- |
596| callbackFn | function | 是 | 回调函数。 |
597| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
598
599callbackfn的参数说明:
600| 参数名 | 类型 | 必填 | 说明 |
601| -------- | -------- | -------- | -------- |
602| value | T | 否 | 当前遍历到的元素键值对的值,默认值为首个键值对的值。 |
603| key | T | 否 | 当前遍历到的元素键值对的键(和value相同),默认值为首个键值对的键。 |
604| set | LightWeightSet&lt;T&gt; | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
605
606**错误码:**
607
608以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
609
610| 错误码ID | 错误信息 |
611| -------- | -------- |
612| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
613| 10200011 | The forEach method cannot be bound. |
614
615**示例:**
616
617```ts
618let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
619lightWeightSet.add("sparrow");
620lightWeightSet.add("gull");
621lightWeightSet.forEach((value ?: string, key ?: string) => {
622  console.log("value:" + value, "key:" + key);
623});
624```
625```ts
626// 不建议在forEach函数中使用add、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
627let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
628for(let i = 0; i < 10; i++) {
629  lightWeightSet.add(i + "123");
630}
631for(let i = 0; i < 10; i++) {
632  lightWeightSet.remove(i + "123");
633}
634```
635
636### entries
637
638entries(): IterableIterator<[T, T]>
639
640返回包含此映射中包含的键值对的新迭代器对象。
641
642**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
643
644**系统能力:** SystemCapability.Utils.Lang
645
646**返回值:**
647
648| 类型 | 说明 |
649| -------- | -------- |
650| IterableIterator<[T, T]> | 返回一个迭代器。 |
651
652**错误码:**
653
654以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
655
656| 错误码ID | 错误信息 |
657| -------- | -------- |
658| 10200011 | The entries method cannot be bound. |
659
660**示例:**
661
662```ts
663let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
664lightWeightSet.add("squirrel");
665lightWeightSet.add("sparrow");
666let iter = lightWeightSet.entries();
667let index = 0;
668while(index < lightWeightSet.length) {
669  console.log(JSON.stringify(iter.next().value));
670  index++;
671}
672```
673```ts
674// 不建议在entries中使用add、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
675let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
676for(let i = 0; i < 10; i++) {
677  lightWeightSet.add(i + "123");
678}
679for(let i = 0; i < 10; i++) {
680  lightWeightSet.remove(i + "123");
681}
682```
683
684### [Symbol.iterator]
685
686[Symbol.iterator]\(): IterableIterator&lt;T&gt;
687
688返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
689
690**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
691
692**系统能力:** SystemCapability.Utils.Lang
693
694**返回值:**
695
696| 类型 | 说明 |
697| -------- | -------- |
698| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
699
700**错误码:**
701
702以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
703
704| 错误码ID | 错误信息 |
705| -------- | -------- |
706| 10200011 | The Symbol.iterator method cannot be bound. |
707
708**示例:**
709
710```ts
711let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
712lightWeightSet.add("squirrel");
713lightWeightSet.add("sparrow");
714
715// 使用方法一:
716let nums: Array<string> = lightWeightSet.toArray()
717for (let item of nums) {
718  console.log("value:" + item);
719}
720
721// 使用方法二:
722let iter = lightWeightSet[Symbol.iterator]();
723let temp: IteratorResult<string> = iter.next();
724while(!temp.done) {
725  console.log("value:" + temp.value);
726  temp = iter.next();
727}
728```
729```ts
730// 不建议在Symbol.iterator中使用add、remove、removeAt方法,会导致死循环等不可预知的风险,可使用for循环来进行插入和删除。
731let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
732for(let i = 0; i < 10; i++) {
733  lightWeightSet.add(i + "123");
734}
735for(let i = 0; i < 10; i++) {
736  lightWeightSet.remove(i + "123");
737}
738```
739
740
741### equal<sup>(deprecated)</sup>
742
743equal(obj: Object): boolean
744
745判断此容器与obj的构成元素是否相同。
746
747> **说明:**
748>
749> 此接口从API version 10开始支持,从API version 12开始废弃。无替代接口。
750
751**系统能力:** SystemCapability.Utils.Lang
752
753**参数:**
754
755| 参数名 | 类型 | 必填 | 说明 |
756| -------- | -------- | -------- | -------- |
757| obj | Object | 是 | 比较对象。 |
758
759**返回值:**
760
761| 类型 | 说明 |
762| -------- | -------- |
763| boolean | 当obj为仅含string或number的LightWeightSet或数组,且对象内部元素构成相同时,返回true;其它情况返回false。 |
764
765**错误码:**
766
767以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
768
769| 错误码ID | 错误信息 |
770| -------- | -------- |
771| 10200011 | The equal method cannot be bound. |
772
773**示例:**
774
775```ts
776let lightWeightSet: LightWeightSet<string> = new LightWeightSet();
777lightWeightSet.add("squirrel");
778lightWeightSet.add("sparrow");
779let obj = ["sparrow", "squirrel"];
780let result = lightWeightSet.equal(obj);
781```