• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.LightWeightSet (Nonlinear Container LightWeightSet)
2
3> **NOTE**
4> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
5
6**LightWeightSet** stores a set of values, each of which must be unique.
7
8**LightWeightSet** is based on generics and uses a lightweight structure. Its default initial capacity is 8, and it has the capacity doubled in each expansion.
9
10The values in such a set are searched using hash values, which are stored in an array.
11
12Compared with **[HashSet](js-apis-hashset.md)**, which can also store values, **LightWeightSet** occupies less memory.
13
14**Recommended use case**: Use **LightWeightSet** when you need a set that has only unique elements or need to deduplicate a set.
15
16This topic uses the following to identify the use of generics:
17- T: Type
18
19## Modules to Import
20
21```ts
22import LightWeightSet from '@ohos.util.LightWeightSet';
23```
24
25## LightWeightSet
26
27### Attributes
28
29**System capability**: SystemCapability.Utils.Lang
30
31| Name| Type| Readable| Writable| Description|
32| -------- | -------- | -------- | -------- | -------- |
33| length | number | Yes| No| Number of elements in a lightweight set (called container later).|
34
35
36### constructor
37
38constructor()
39
40A constructor used to create a **LightWeightSet** instance.
41
42**System capability**: SystemCapability.Utils.Lang
43
44**Error codes**
45
46For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
47
48| ID| Error Message|
49| -------- | -------- |
50| 10200012 | The LightWeightSet's constructor cannot be directly invoked. |
51
52**Example**
53
54```ts
55let lightWeightSet = new LightWeightSet();
56```
57
58
59### isEmpty
60
61isEmpty(): boolean
62
63Checks whether this container is empty (contains no element).
64
65**System capability**: SystemCapability.Utils.Lang
66
67**Return value**
68
69| Type| Description|
70| -------- | -------- |
71| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
72
73**Error codes**
74
75For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
76
77| ID| Error Message|
78| -------- | -------- |
79| 10200011 | The isEmpty method cannot be bound. |
80
81**Example**
82
83```ts
84const lightWeightSet = new LightWeightSet();
85let result = lightWeightSet.isEmpty();
86```
87
88### add
89
90add(obj: T): boolean
91
92Adds an element to this container.
93
94**System capability**: SystemCapability.Utils.Lang
95
96**Parameters**
97
98| Name| Type| Mandatory| Description|
99| -------- | -------- | -------- | -------- |
100| obj | T | Yes| Target element.|
101
102**Return value**
103
104| Type| Description|
105| -------- | -------- |
106| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
107
108**Error codes**
109
110For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
111
112| ID| Error Message|
113| -------- | -------- |
114| 10200011 | The add method cannot be bound. |
115
116**Example**
117
118```ts
119let lightWeightSet = new LightWeightSet();
120let result = lightWeightSet.add("squirrel");
121```
122
123
124### addAll
125
126addAll(set: LightWeightSet<T>): boolean
127
128Adds all elements in a **LightWeightSet** instance to this container.
129
130**System capability**: SystemCapability.Utils.Lang
131
132**Parameters**
133
134| Name| Type| Mandatory| Description|
135| -------- | -------- | -------- | -------- |
136| set | LightWeightSet<T> | Yes| **LightWeightSet** instance whose elements are to be added to the current container.|
137
138**Error codes**
139
140For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
141
142| ID| Error Message|
143| -------- | -------- |
144| 10200011 | The addAll method cannot be bound. |
145
146**Example**
147
148```ts
149let lightWeightSet = new LightWeightSet();
150lightWeightSet.add("squirrel");
151lightWeightSet.add("sparrow");
152let set = new LightWeightSet();
153set.add("gull");
154let result = lightWeightSet.addAll(set);
155```
156
157
158### hasAll
159
160hasAll(set: LightWeightSet<T>): boolean
161
162Checks whether this container contains all elements of the specified **LightWeightSet** instance.
163
164**System capability**: SystemCapability.Utils.Lang
165
166**Parameters**
167
168| Name| Type| Mandatory| Description|
169| -------- | -------- | -------- | -------- |
170| set | LightWeightSet<T> | Yes| **LightWeightSet** instance to be used for comparison.|
171
172**Return value**
173
174| Type| Description|
175| -------- | -------- |
176| boolean | Returns **true** if all the elements in the specified **LightWeightSet** instance are contained; returns **false** otherwise.|
177
178**Error codes**
179
180For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
181
182| ID| Error Message|
183| -------- | -------- |
184| 10200011 | The hasAll method cannot be bound. |
185
186**Example**
187
188```ts
189let lightWeightSet = new LightWeightSet();
190lightWeightSet.add("squirrel");
191lightWeightSet.add("sparrow");
192let set = new LightWeightSet();
193set.add("sparrow");
194let result = lightWeightSet.hasAll(set);
195```
196
197
198### has
199
200has(key: T): boolean
201
202Checks whether this container has the specified key.
203
204**System capability**: SystemCapability.Utils.Lang
205
206**Parameters**
207
208| Name| Type| Mandatory| Description|
209| -------- | -------- | -------- | -------- |
210| key| T | Yes| Target key.|
211
212**Return value**
213
214| Type| Description|
215| -------- | -------- |
216| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
217
218**Error codes**
219
220For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
221
222| ID| Error Message|
223| -------- | -------- |
224| 10200011 | The has method cannot be bound. |
225
226**Example**
227
228```ts
229let lightWeightSet = new LightWeightSet();
230let result = lightWeightSet.has(123);
231lightWeightSet.add(123);
232result = lightWeightSet.has(123);
233```
234
235
236### equal
237
238equal(obj: Object): boolean
239
240Checks whether this container contains objects of the same type as the specified **obj**.
241
242**System capability**: SystemCapability.Utils.Lang
243
244**Parameters**
245
246| Name| Type| Mandatory| Description|
247| -------- | -------- | -------- | -------- |
248| obj | Object | Yes| **LightWeightSet** instance to be used for comparison.|
249
250**Return value**
251
252| Type| Description|
253| -------- | -------- |
254| boolean | Returns **true** if the container contains objects of the same type as the specified **obj**; returns **false** otherwise.|
255
256**Error codes**
257
258For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
259
260| ID| Error Message|
261| -------- | -------- |
262| 10200011 | The equal method cannot be bound. |
263
264**Example**
265
266```ts
267let lightWeightSet = new LightWeightSet();
268lightWeightSet.add("squirrel");
269lightWeightSet.add("sparrow");
270let obj = ["squirrel", "sparrow"];
271let result = lightWeightSet.equal(obj);
272```
273
274
275### increaseCapacityTo
276
277increaseCapacityTo(minimumCapacity: number): void
278
279Increases the capacity of this container.
280
281**System capability**: SystemCapability.Utils.Lang
282
283**Parameters**
284
285| Name| Type| Mandatory| Description|
286| -------- | -------- | -------- | -------- |
287| minimumCapacity | number | Yes| Minimum number of elements to accommodate in the container.|
288
289**Error codes**
290
291For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
292
293| ID| Error Message|
294| -------- | -------- |
295| 10200011 | The increaseCapacityTo method cannot be bound. |
296| 10200001 | The value of minimumCapacity is out of range. |
297
298**Example**
299
300```ts
301let lightWeightSet = new LightWeightSet();
302lightWeightSet.increaseCapacityTo(10);
303```
304
305
306### getIndexOf
307
308getIndexOf(key: T): number
309
310Obtains the position index of the element with the specified key in this container.
311
312**System capability**: SystemCapability.Utils.Lang
313
314**Parameters**
315
316| Name| Type| Mandatory| Description|
317| -------- | -------- | -------- | -------- |
318| key| T | Yes| Key of the target element.|
319
320**Return value**
321
322| Type| Description|
323| -------- | -------- |
324| number | Position index of the element.|
325
326**Error codes**
327
328For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
329
330| ID| Error Message|
331| -------- | -------- |
332| 10200011 | The getIndexOf method cannot be bound. |
333
334**Example**
335
336```ts
337let lightWeightSet = new LightWeightSet();
338lightWeightSet.add("squirrel");
339lightWeightSet.add("sparrow");
340let result = lightWeightSet.getIndexOf("sparrow");
341```
342
343
344### remove
345
346remove(key: T): T
347
348Removes an element of the specified key from this container.
349
350**System capability**: SystemCapability.Utils.Lang
351
352**Parameters**
353
354| Name| Type| Mandatory| Description|
355| -------- | -------- | -------- | -------- |
356| key| T | Yes| Key of the target element.|
357
358**Return value**
359
360| Type| Description|
361| -------- | -------- |
362| T | Value of the element removed.|
363
364**Error codes**
365
366For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
367
368| ID| Error Message|
369| -------- | -------- |
370| 10200011 | The remove method cannot be bound. |
371
372**Example**
373
374```ts
375let lightWeightSet = new LightWeightSet();
376lightWeightSet.add("squirrel");
377lightWeightSet.add("sparrow");
378let result = lightWeightSet.remove("sparrow");
379```
380
381
382### removeAt
383
384removeAt(index: number): boolean
385
386Removes the element at the specified position from this container.
387
388**System capability**: SystemCapability.Utils.Lang
389
390**Parameters**
391
392| Name| Type| Mandatory| Description|
393| -------- | -------- | -------- | -------- |
394| index | number | Yes| Position index of the element.|
395
396**Return value**
397
398| Type| Description|
399| -------- | -------- |
400| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
401
402**Error codes**
403
404For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
405
406| ID| Error Message|
407| -------- | -------- |
408| 10200011 | The removeAt method cannot be bound. |
409
410**Example**
411
412```ts
413let lightWeightSet = new LightWeightSet();
414lightWeightSet.add("squirrel");
415lightWeightSet.add("sparrow");
416let result = lightWeightSet.removeAt(1);
417```
418
419
420### getValueAt
421
422getValueAt(index: number): T
423
424Obtains the value of the element at the specified position in this container.
425
426**System capability**: SystemCapability.Utils.Lang
427
428**Parameters**
429
430| Name| Type| Mandatory| Description|
431| -------- | -------- | -------- | -------- |
432| index | number | Yes| Position index of the element.|
433
434**Return value**
435
436| Type| Description|
437| -------- | -------- |
438| T | Value obtained.|
439
440**Error codes**
441
442For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
443
444| ID| Error Message|
445| -------- | -------- |
446| 10200011 | The getValueAt method cannot be bound. |
447
448**Parameters**
449
450```ts
451let lightWeightSet = new LightWeightSet();
452lightWeightSet.add("squirrel");
453lightWeightSet.add("sparrow");
454let result = lightWeightSet.getValueAt(1);
455```
456
457
458### clear
459
460clear(): void
461
462Clears this container and sets its length to **0**.
463
464**System capability**: SystemCapability.Utils.Lang
465
466**Error codes**
467
468For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
469
470| ID| Error Message|
471| -------- | -------- |
472| 10200011 | The clear method cannot be bound. |
473
474**Example**
475
476```ts
477let lightWeightSet = new LightWeightSet();
478lightWeightSet.add("squirrel");
479lightWeightSet.add("sparrow");
480lightWeightSet.clear();
481```
482
483
484### toString
485
486toString(): String
487
488Obtains a string that contains all elements in this container.
489
490**System capability**: SystemCapability.Utils.Lang
491
492**Return value**
493
494| Type| Description|
495| -------- | -------- |
496| String | String obtained.|
497
498**Example**
499
500```ts
501let lightWeightSet = new LightWeightSet();
502lightWeightSet.add("squirrel");
503lightWeightSet.add("sparrow");
504let result = lightWeightSet.toString();
505```
506
507
508### toArray
509
510toArray(): Array<T>
511
512Obtains an array that contains all objects in this container.
513
514**System capability**: SystemCapability.Utils.Lang
515
516**Return value**
517
518| Type| Description|
519| -------- | -------- |
520| Array<T> | Array obtained.|
521
522**Error codes**
523
524For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
525
526| ID| Error Message|
527| -------- | -------- |
528| 10200011 | The toArray method cannot be bound. |
529
530**Example**
531
532```ts
533let lightWeightSet = new LightWeightSet();
534lightWeightSet.add("squirrel");
535lightWeightSet.add("sparrow");
536let result = lightWeightSet.toArray();
537```
538
539
540### values
541
542values(): IterableIterator<T>
543
544Obtains an iterator that contains all the values in this container.
545
546**System capability**: SystemCapability.Utils.Lang
547
548**Return value**
549
550| Type| Description|
551| -------- | -------- |
552| IterableIterator<T> | Iterator obtained.|
553
554**Error codes**
555
556For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
557
558| ID| Error Message|
559| -------- | -------- |
560| 10200011 | The values method cannot be bound. |
561
562**Example**
563
564```ts
565let lightWeightSet = new LightWeightSet();
566lightWeightSet.add("squirrel");
567lightWeightSet.add("sparrow");
568let iter = lightWeightSet.values();
569let index = 0;
570while(index < lightWeightSet.length) {
571  console.log(JSON.stringify(iter.next().value));
572  index++;
573}
574```
575
576
577### forEach
578
579forEach(callbackFn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
580
581Uses a callback to traverse the elements in this container and obtain their position indexes.
582
583**System capability**: SystemCapability.Utils.Lang
584
585**Parameters**
586
587| Name| Type| Mandatory| Description|
588| -------- | -------- | -------- | -------- |
589| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
590| thisArg | Object | No| Value to use when the callback is invoked.|
591
592callbackfn
593| Name| Type| Mandatory| Description|
594| -------- | -------- | -------- | -------- |
595| value | T | No| Value of the element that is currently traversed.|
596| key| T | No| Key of the element that is currently traversed (same as **value**).|
597| set | LightWeightSet&lt;T&gt; | No| Instance that invokes the **forEach** method.|
598
599**Error codes**
600
601For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
602
603| ID| Error Message|
604| -------- | -------- |
605| 10200011 | The forEach method cannot be bound. |
606
607**Example**
608
609```ts
610let lightWeightSet = new LightWeightSet();
611lightWeightSet.add("sparrow");
612lightWeightSet.add("gull");
613lightWeightSet.forEach((value, key) => {
614    console.log("value:" + value, "key:" + key);
615});
616```
617
618
619### entries
620
621entries(): IterableIterator<[T, T]>
622
623Obtains an iterator that contains all the elements in this container.
624
625**System capability**: SystemCapability.Utils.Lang
626
627**Return value**
628
629| Type| Description|
630| -------- | -------- |
631| IterableIterator<[T, T]> | Iterator obtained.|
632
633**Error codes**
634
635For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
636
637| ID| Error Message|
638| -------- | -------- |
639| 10200011 | The entries method cannot be bound. |
640
641**Example**
642
643```ts
644let lightWeightSet = new LightWeightSet();
645lightWeightSet.add("squirrel");
646lightWeightSet.add("sparrow");
647let iter = lightWeightSet.entries();
648let index = 0;
649while(index < lightWeightSet.length) {
650  console.log(JSON.stringify(iter.next().value));
651  index++;
652}
653```
654
655
656### [Symbol.iterator]
657
658[Symbol.iterator]\(): IterableIterator&lt;T&gt;
659
660Obtains an iterator, each item of which is a JavaScript object.
661
662**System capability**: SystemCapability.Utils.Lang
663
664**Return value**
665
666| Type| Description|
667| -------- | -------- |
668| IterableIterator&lt;T&gt; | Iterator obtained.|
669
670**Error codes**
671
672For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
673
674| ID| Error Message|
675| -------- | -------- |
676| 10200011 | The Symbol.iterator method cannot be bound. |
677
678**Example**
679
680```ts
681let lightWeightSet = new LightWeightSet();
682lightWeightSet.add("squirrel");
683lightWeightSet.add("sparrow");
684
685// Method 1:
686for (let item of lightWeightSet) {
687  console.log("value:" + item);
688}
689
690// Method 2:
691let iter = lightWeightSet[Symbol.iterator]();
692let temp = iter.next().value;
693while(temp != undefined) {
694  console.log("value:" + temp);
695  temp = iter.next().value;
696}
697```
698