js-apis-lightweightset.md 11.1 KB
Newer Older
L
linhaoran 已提交
1 2 3 4 5
# 非线性容器LightWeightSet  

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

6 7 8 9 10 11 12 13 14
LightWeightSet可用于存储一系列值的集合,存储元素中value值唯一。

LightWeightSet依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。

集合中value值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的value值。

LightWeightSet和[HashSet](js-apis-hashset.md)都是用来存储键值的集合,LightWeightSet的占用内存更小。

**推荐使用场景:** 当需要存取某个集合或是对某个集合去重时,推荐使用占用内存更小的LightWeightSet。
L
linhaoran 已提交
15 16 17

## 导入模块

18
```ts
19
import LightWeightSet from '@ohos.util.LightWeightSet';  
L
linhaoran 已提交
20 21
```

Z
zengyawen 已提交
22
## 系统能力
L
linhaoran 已提交
23

Z
zengyawen 已提交
24
SystemCapability.Utils.Lang
L
linhaoran 已提交
25 26 27 28 29 30 31 32

## LightWeightSet


### 属性

| 名称 | 参数类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
Z
zengyawen 已提交
33
| length | number | 是 | 否 | LightWeightSet的元素个数。 |
L
linhaoran 已提交
34 35 36 37


### constructor

Z
zengyawen 已提交
38
constructor()
L
linhaoran 已提交
39 40 41

LightWeightSet的构造函数。

Z
zengyawen 已提交
42 43
**示例:**

44
```ts
Z
zengyawen 已提交
45 46
let lightWeightSet = new LightWeightSet();
```
L
linhaoran 已提交
47 48 49 50


### isEmpty

Z
zengyawen 已提交
51
isEmpty(): boolean
L
linhaoran 已提交
52 53 54

判断该容器是否为空。

Z
zengyawen 已提交
55 56 57 58 59
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |
L
linhaoran 已提交
60

Z
zengyawen 已提交
61 62
**示例:**

63
```ts
Z
zengyawen 已提交
64
const lightWeightSet = new LightWeightSet();
65
let result = lightWeightSet.isEmpty();
Z
zengyawen 已提交
66
```
L
linhaoran 已提交
67 68 69

### add

70
add(obj: T): boolean
L
linhaoran 已提交
71 72 73

向此容器中添加数据。

Z
zengyawen 已提交
74 75 76 77
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
78
| obj | T | 是 | 添加的成员数据。 |
Z
zengyawen 已提交
79 80 81 82 83 84

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 成功添加元素返回true,否则返回false。 |
L
linhaoran 已提交
85

Z
zengyawen 已提交
86
**示例:**
L
linhaoran 已提交
87

88
```ts
Z
zengyawen 已提交
89
let lightWeightSet = new LightWeightSet();
90
let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
Z
zengyawen 已提交
91
```
L
linhaoran 已提交
92 93 94 95


### addAll

Z
zengyawen 已提交
96
addAll(set: LightWeightSet<T>): boolean
L
linhaoran 已提交
97 98 99

将另一个容器中的所有元素组添加到当前容器中。

Z
zengyawen 已提交
100 101 102 103
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
104
| set | LightWeightSet<T> | 是 | 提供添加元素的lightWeightSet。 |
L
linhaoran 已提交
105

Z
zengyawen 已提交
106 107
**示例:**

108
```ts
Z
zengyawen 已提交
109 110 111 112 113
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let set = new LightWeightSet();
set.add("sfage");
114
let result = lightWeightSet.addAll(set);
Z
zengyawen 已提交
115
```
L
linhaoran 已提交
116 117 118 119


### hasAll

Z
zengyawen 已提交
120
hasAll(set: LightWeightSet<T>): boolean
L
linhaoran 已提交
121 122 123

判断此容器中是否含有该指定set中的所有元素。

Z
zengyawen 已提交
124
**参数:**
L
linhaoran 已提交
125

Z
zengyawen 已提交
126 127 128
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| set | LightWeightSet<T> | 是 | 比较对象。 |
L
linhaoran 已提交
129

Z
zengyawen 已提交
130 131 132 133 134 135 136 137
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含所有元素返回true,否则返回false。 |

**示例:**

138
```ts
Z
zengyawen 已提交
139 140 141 142 143 144 145
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let set = new LightWeightSet();
set.add("sdfs");
let result = lightWeightSet.hasAll(set);
```
L
linhaoran 已提交
146 147 148 149


### has

150
has(key: T): boolean
L
linhaoran 已提交
151

152
判断此容器中是否含有该指定key。
L
linhaoran 已提交
153

Z
zengyawen 已提交
154
**参数:**
L
linhaoran 已提交
155

Z
zengyawen 已提交
156 157
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
158
| key | T | 是 | 指定key |
L
linhaoran 已提交
159

Z
zengyawen 已提交
160 161 162 163
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
164
| boolean | 包含指定key返回true,否则返回false。 |
Z
zengyawen 已提交
165 166 167

**示例:**

168
```ts
Z
zengyawen 已提交
169
let lightWeightSet = new LightWeightSet();
170
let result = lightWeightSet.has(123);
Z
zengyawen 已提交
171
lightWeightSet.add(123);
172
result = lightWeightSet.has(123);
Z
zengyawen 已提交
173
```
L
linhaoran 已提交
174 175 176 177


### equal

Z
zengyawen 已提交
178 179 180
equal(obj: Object): boolean

判断此容器中是否含有该指定obj同类型的对象。
L
linhaoran 已提交
181

Z
zengyawen 已提交
182
**参数:**
L
linhaoran 已提交
183

Z
zengyawen 已提交
184 185 186
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| obj | Object | 是 | 比较对象。 |
L
linhaoran 已提交
187

Z
zengyawen 已提交
188
**返回值:**
L
linhaoran 已提交
189

Z
zengyawen 已提交
190 191 192 193 194 195
| 类型 | 说明 |
| -------- | -------- |
| boolean | 构成类型相同返回true,否则返回false。 |

**示例:**

196
```ts
Z
zengyawen 已提交
197 198 199
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
200
let obj = ["Ahfbrgrbgnutfodgorrogorgrogofdfdf", "sdfs"];
Z
zengyawen 已提交
201 202
let result = lightWeightSet.equal(obj);
```
L
linhaoran 已提交
203 204


205
### increaseCapacityTo
L
linhaoran 已提交
206

207
increaseCapacityTo(minimumCapacity: number): void
L
linhaoran 已提交
208 209 210

将当前容器扩容至可以容纳指定数量元素。

Z
zengyawen 已提交
211
**参数:**
L
linhaoran 已提交
212

Z
zengyawen 已提交
213 214 215
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | 是 | 需要容纳数量。 |
L
linhaoran 已提交
216

Z
zengyawen 已提交
217 218
**示例:**

219
```ts
Z
zengyawen 已提交
220
let lightWeightSet = new LightWeightSet();
221
lightWeightSet.increaseCapacityTo(10);
Z
zengyawen 已提交
222
```
L
linhaoran 已提交
223 224 225 226


### getIndexOf

Z
zengyawen 已提交
227
getIndexOf(key: T): number
L
linhaoran 已提交
228 229 230

获取指定key所对应的下标。

Z
zengyawen 已提交
231 232 233 234 235 236 237 238 239 240
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | T | 是 | 查找的指定key。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
241
| number | 在lightWeightSet中指定数据的下标。 |
L
linhaoran 已提交
242

Z
zengyawen 已提交
243
**示例:**
L
linhaoran 已提交
244

245
```ts
Z
zengyawen 已提交
246 247 248
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
249
let result = lightWeightSet.getIndexOf("sdfs");
Z
zengyawen 已提交
250
```
L
linhaoran 已提交
251 252 253 254


### remove

Z
zengyawen 已提交
255
remove(key: T): T
L
linhaoran 已提交
256

257
删除并返回指定key对应的元素。
L
linhaoran 已提交
258

Z
zengyawen 已提交
259 260 261 262
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
263
| key | T | 是 | 指定key。 |
Z
zengyawen 已提交
264 265 266 267 268 269

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| T | 返回删除元素的值。 |
L
linhaoran 已提交
270

Z
zengyawen 已提交
271
**示例:**
L
linhaoran 已提交
272

273
```ts
Z
zengyawen 已提交
274 275 276
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
277
let result = lightWeightSet.remove("sdfs");
Z
zengyawen 已提交
278
```
L
linhaoran 已提交
279 280 281 282


### removeAt

Z
zengyawen 已提交
283
removeAt(index: number): boolean
L
linhaoran 已提交
284

285
删除指定下标所对应的元素。
L
linhaoran 已提交
286

Z
zengyawen 已提交
287 288 289 290
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
291
| index | number | 是 | 指定下标。 |
L
linhaoran 已提交
292

Z
zengyawen 已提交
293
**返回值:**
L
linhaoran 已提交
294

Z
zengyawen 已提交
295 296 297 298 299 300
| 类型 | 说明 |
| -------- | -------- |
| boolean | 确认是否成功删除元素 |

**示例:**

301
```ts
Z
zengyawen 已提交
302 303 304
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
305
let result = lightWeightSet.removeAt(1);
Z
zengyawen 已提交
306
```
L
linhaoran 已提交
307 308 309 310


### getValueAt

Z
zengyawen 已提交
311 312
getValueAt(index: number): T

313
获取此容器中指定下标对应的元素。
Z
zengyawen 已提交
314 315

**参数:**
L
linhaoran 已提交
316

Z
zengyawen 已提交
317 318
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
319
| index | number | 是 | 指定下标。 |
L
linhaoran 已提交
320

Z
zengyawen 已提交
321
**返回值:**
L
linhaoran 已提交
322

Z
zengyawen 已提交
323 324
| 类型 | 说明 |
| -------- | -------- |
325
| T | 返回指定下标对应的元素。 |
L
linhaoran 已提交
326

Z
zengyawen 已提交
327 328
**参数:**

329
```ts
Z
zengyawen 已提交
330 331 332
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
333
let result = lightWeightSet.getValueAt(1);
Z
zengyawen 已提交
334
```
L
linhaoran 已提交
335 336 337 338


### clear

Z
zengyawen 已提交
339 340 341
clear(): void

清除容器中的所有元素,并把length置为0。
L
linhaoran 已提交
342

Z
zengyawen 已提交
343
**示例:**
L
linhaoran 已提交
344

345
```ts
Z
zengyawen 已提交
346 347 348 349 350
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
lightWeightSet.clear();
```
L
linhaoran 已提交
351 352 353 354


### toString

Z
zengyawen 已提交
355
toString(): String
L
linhaoran 已提交
356 357 358

获取包含容器中所有键和值的字符串。

Z
zengyawen 已提交
359
**返回值:**
L
linhaoran 已提交
360

Z
zengyawen 已提交
361 362 363 364 365 366
| 类型 | 说明 |
| -------- | -------- |
| String | 返回对应字符串。 |

**示例:**

367
```ts
Z
zengyawen 已提交
368 369 370
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
371
let result = lightWeightSet.toString();
Z
zengyawen 已提交
372
```
L
linhaoran 已提交
373 374 375 376


### toArray

Z
zengyawen 已提交
377
toArray(): Array<T>
L
linhaoran 已提交
378 379 380

获取包含此容器中所有对象的数组。

Z
zengyawen 已提交
381 382 383 384 385
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Array<T> | 返回对应数组。 |
L
linhaoran 已提交
386

Z
zengyawen 已提交
387 388
**示例:**

389
```ts
Z
zengyawen 已提交
390 391 392
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
393
let result = lightWeightSet.toArray();
Z
zengyawen 已提交
394
```
L
linhaoran 已提交
395 396 397 398


### values

Z
zengyawen 已提交
399
values(): IterableIterator<T>
L
linhaoran 已提交
400

401
返回包含此映射中包含的键值的新迭代器对象。
L
linhaoran 已提交
402

Z
zengyawen 已提交
403 404 405 406 407 408 409 410
**返回值:**

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

**示例:**

411
```ts
Z
zengyawen 已提交
412 413 414 415 416 417 418 419 420 421
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let iter = lightWeightSet.values();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
```
L
linhaoran 已提交
422 423 424 425


### forEach

426
forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet&lt;T&gt;) => void, thisArg?: Object): void
L
linhaoran 已提交
427 428 429

通过回调函数来遍历LightWeightSet实例对象上的元素以及元素对应的下标。

Z
zengyawen 已提交
430 431 432 433 434 435 436 437 438 439
**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackfn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值。 |

callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
440
| value | T | 否 | 当前遍历到的元素。 |
Z
zengyawen 已提交
441
| key | T | 否 | 当前遍历到的元素(和value相同)。 |
442
| set | LightWeightSet&lt;T&gt; | 否 | 当前调用forEach方法的实例对象。 |
Z
zengyawen 已提交
443 444 445

**示例:**

446
```ts
Z
zengyawen 已提交
447 448 449 450
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("sdfs");
lightWeightSet.add("dfsghsf");
lightWeightSet.forEach((value, key) => {
451
  console.log("value:" + value, key);
Z
zengyawen 已提交
452 453
});
```
L
linhaoran 已提交
454 455 456 457


### entries

Z
zengyawen 已提交
458
entries(): IterableIterator<[T, T]>
L
linhaoran 已提交
459

460
返回包含此映射中包含的键值对的新迭代器对象。
L
linhaoran 已提交
461

Z
zengyawen 已提交
462 463 464 465 466 467 468 469
**返回值:**

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

**示例:**

470
```ts
Z
zengyawen 已提交
471 472 473 474 475 476 477 478 479 480
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");
let iter = lightWeightSet.entries();
let index = 0;
while(index < lightWeightSet.length) {
  console.log(JSON.stringify(iter.next().value));
  index++;
}
```
L
linhaoran 已提交
481 482 483 484


### [Symbol.iterator]

Z
zengyawen 已提交
485
[Symbol.iterator]\(): IterableIterator&lt;T&gt;
L
linhaoran 已提交
486 487 488

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

Z
zengyawen 已提交
489 490 491 492 493 494 495 496
**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| IterableIterator&lt;T&gt; | 返回一个迭代器。 |

**示例:**

497
```ts
Z
zengyawen 已提交
498 499 500 501 502 503
let lightWeightSet = new LightWeightSet();
lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightSet.add("sdfs");

// 使用方法一:
for (let item of lightWeightSet) { 
504
  console.log("value:" + item);
Z
zengyawen 已提交
505 506 507 508 509 510
}

// 使用方法二:
let iter = lightWeightSet[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
511
  console.log("value:" + temp);
Z
zengyawen 已提交
512 513 514
  temp = iter.next().value;
}
```