1# 非线性容器LightWeightSet 2 3>  **说明:** 4> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 5 6LightWeightSet可用于存储一系列值的集合,存储元素中value值唯一。 7 8LightWeightSet依据泛型定义,采用轻量级结构,初始默认容量大小为8,每次扩容大小为原始容量的两倍。 9 10集合中value值的查找依赖于hash算法,通过一个数组存储hash值,然后映射到其他数组中的value值。 11 12LightWeightSet和[HashSet](js-apis-hashset.md)都是用来存储键值的集合,LightWeightSet的占用内存更小。 13 14**推荐使用场景:** 当需要存取某个集合或是对某个集合去重时,推荐使用占用内存更小的LightWeightSet。 15 16## 导入模块 17 18```ts 19import LightWeightSet from '@ohos.util.LightWeightSet'; 20``` 21 22 23 24## LightWeightSet 25 26### 属性 27 28**系统能力:** SystemCapability.Utils.Lang 29 30| 名称 | 参数类型 | 可读 | 可写 | 说明 | 31| -------- | -------- | -------- | -------- | -------- | 32| length | number | 是 | 否 | LightWeightSet的元素个数。 | 33 34 35### constructor 36 37constructor() 38 39LightWeightSet的构造函数。 40 41**系统能力:** SystemCapability.Utils.Lang 42 43**示例:** 44 45```ts 46let lightWeightSet = new LightWeightSet(); 47``` 48 49 50### isEmpty 51 52isEmpty(): boolean 53 54判断该容器是否为空。 55 56**系统能力:** SystemCapability.Utils.Lang 57 58**返回值:** 59 60| 类型 | 说明 | 61| -------- | -------- | 62| boolean | 为空返回true,不为空返回false。 | 63 64**示例:** 65 66```ts 67const lightWeightSet = new LightWeightSet(); 68let result = lightWeightSet.isEmpty(); 69``` 70 71### add 72 73add(obj: T): boolean 74 75向此容器中添加数据。 76 77**系统能力:** SystemCapability.Utils.Lang 78 79**参数:** 80 81| 参数名 | 类型 | 必填 | 说明 | 82| -------- | -------- | -------- | -------- | 83| obj | T | 是 | 添加的成员数据。 | 84 85**返回值:** 86 87| 类型 | 说明 | 88| -------- | -------- | 89| boolean | 成功添加元素返回true,否则返回false。 | 90 91**示例:** 92 93```ts 94let lightWeightSet = new LightWeightSet(); 95let result = lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 96``` 97 98 99### addAll 100 101addAll(set: LightWeightSet<T>): boolean 102 103将另一个容器中的所有元素组添加到当前容器中。 104 105**系统能力:** SystemCapability.Utils.Lang 106 107**参数:** 108 109| 参数名 | 类型 | 必填 | 说明 | 110| -------- | -------- | -------- | -------- | 111| set | LightWeightSet<T> | 是 | 提供添加元素的lightWeightSet。 | 112 113**示例:** 114 115```ts 116let lightWeightSet = new LightWeightSet(); 117lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 118lightWeightSet.add("sdfs"); 119let set = new LightWeightSet(); 120set.add("sfage"); 121let result = lightWeightSet.addAll(set); 122``` 123 124 125### hasAll 126 127hasAll(set: LightWeightSet<T>): boolean 128 129判断此容器中是否含有该指定set中的所有元素。 130 131**系统能力:** SystemCapability.Utils.Lang 132 133**参数:** 134 135| 参数名 | 类型 | 必填 | 说明 | 136| -------- | -------- | -------- | -------- | 137| set | LightWeightSet<T> | 是 | 比较对象。 | 138 139**返回值:** 140 141| 类型 | 说明 | 142| -------- | -------- | 143| boolean | 包含所有元素返回true,否则返回false。 | 144 145**示例:** 146 147```ts 148let lightWeightSet = new LightWeightSet(); 149lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 150lightWeightSet.add("sdfs"); 151let set = new LightWeightSet(); 152set.add("sdfs"); 153let result = lightWeightSet.hasAll(set); 154``` 155 156 157### has 158 159has(key: T): boolean 160 161判断此容器中是否含有该指定key。 162 163**系统能力:** SystemCapability.Utils.Lang 164 165**参数:** 166 167| 参数名 | 类型 | 必填 | 说明 | 168| -------- | -------- | -------- | -------- | 169| key | T | 是 | 指定key | 170 171**返回值:** 172 173| 类型 | 说明 | 174| -------- | -------- | 175| boolean | 包含指定key返回true,否则返回false。 | 176 177**示例:** 178 179```ts 180let lightWeightSet = new LightWeightSet(); 181let result = lightWeightSet.has(123); 182lightWeightSet.add(123); 183result = lightWeightSet.has(123); 184``` 185 186 187### equal 188 189equal(obj: Object): boolean 190 191判断此容器中是否含有该指定obj同类型的对象。 192 193**系统能力:** SystemCapability.Utils.Lang 194 195**参数:** 196 197| 参数名 | 类型 | 必填 | 说明 | 198| -------- | -------- | -------- | -------- | 199| obj | Object | 是 | 比较对象。 | 200 201**返回值:** 202 203| 类型 | 说明 | 204| -------- | -------- | 205| boolean | 构成类型相同返回true,否则返回false。 | 206 207**示例:** 208 209```ts 210let lightWeightSet = new LightWeightSet(); 211lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 212lightWeightSet.add("sdfs"); 213let obj = ["Ahfbrgrbgnutfodgorrogorgrogofdfdf", "sdfs"]; 214let result = lightWeightSet.equal(obj); 215``` 216 217 218### increaseCapacityTo 219 220increaseCapacityTo(minimumCapacity: number): void 221 222将当前容器扩容至可以容纳指定数量元素。 223 224**系统能力:** SystemCapability.Utils.Lang 225 226**参数:** 227 228| 参数名 | 类型 | 必填 | 说明 | 229| -------- | -------- | -------- | -------- | 230| minimumCapacity | number | 是 | 需要容纳数量。 | 231 232**示例:** 233 234```ts 235let lightWeightSet = new LightWeightSet(); 236lightWeightSet.increaseCapacityTo(10); 237``` 238 239 240### getIndexOf 241 242getIndexOf(key: T): number 243 244获取指定key所对应的下标。 245 246**系统能力:** SystemCapability.Utils.Lang 247 248**参数:** 249 250| 参数名 | 类型 | 必填 | 说明 | 251| -------- | -------- | -------- | -------- | 252| key | T | 是 | 查找的指定key。 | 253 254**返回值:** 255 256| 类型 | 说明 | 257| -------- | -------- | 258| number | 在lightWeightSet中指定数据的下标。 | 259 260**示例:** 261 262```ts 263let lightWeightSet = new LightWeightSet(); 264lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 265lightWeightSet.add("sdfs"); 266let result = lightWeightSet.getIndexOf("sdfs"); 267``` 268 269 270### remove 271 272remove(key: T): T 273 274删除并返回指定key对应的元素。 275 276**系统能力:** SystemCapability.Utils.Lang 277 278**参数:** 279 280| 参数名 | 类型 | 必填 | 说明 | 281| -------- | -------- | -------- | -------- | 282| key | T | 是 | 指定key。 | 283 284**返回值:** 285 286| 类型 | 说明 | 287| -------- | -------- | 288| T | 返回删除元素的值。 | 289 290**示例:** 291 292```ts 293let lightWeightSet = new LightWeightSet(); 294lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 295lightWeightSet.add("sdfs"); 296let result = lightWeightSet.remove("sdfs"); 297``` 298 299 300### removeAt 301 302removeAt(index: number): boolean 303 304删除指定下标所对应的元素。 305 306**系统能力:** SystemCapability.Utils.Lang 307 308**参数:** 309 310| 参数名 | 类型 | 必填 | 说明 | 311| -------- | -------- | -------- | -------- | 312| index | number | 是 | 指定下标。 | 313 314**返回值:** 315 316| 类型 | 说明 | 317| -------- | -------- | 318| boolean | 确认是否成功删除元素 | 319 320**示例:** 321 322```ts 323let lightWeightSet = new LightWeightSet(); 324lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 325lightWeightSet.add("sdfs"); 326let result = lightWeightSet.removeAt(1); 327``` 328 329 330### getValueAt 331 332getValueAt(index: number): T 333 334获取此容器中指定下标对应的元素。 335 336**系统能力:** SystemCapability.Utils.Lang 337 338**参数:** 339 340| 参数名 | 类型 | 必填 | 说明 | 341| -------- | -------- | -------- | -------- | 342| index | number | 是 | 指定下标。 | 343 344**返回值:** 345 346| 类型 | 说明 | 347| -------- | -------- | 348| T | 返回指定下标对应的元素。 | 349 350**参数:** 351 352```ts 353let lightWeightSet = new LightWeightSet(); 354lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 355lightWeightSet.add("sdfs"); 356let result = lightWeightSet.getValueAt(1); 357``` 358 359 360### clear 361 362clear(): void 363 364清除容器中的所有元素,并把length置为0。 365 366**系统能力:** SystemCapability.Utils.Lang 367 368**示例:** 369 370```ts 371let lightWeightSet = new LightWeightSet(); 372lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 373lightWeightSet.add("sdfs"); 374lightWeightSet.clear(); 375``` 376 377 378### toString 379 380toString(): String 381 382获取包含容器中所有键和值的字符串。 383 384**系统能力:** SystemCapability.Utils.Lang 385 386**返回值:** 387 388| 类型 | 说明 | 389| -------- | -------- | 390| String | 返回对应字符串。 | 391 392**示例:** 393 394```ts 395let lightWeightSet = new LightWeightSet(); 396lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 397lightWeightSet.add("sdfs"); 398let result = lightWeightSet.toString(); 399``` 400 401 402### toArray 403 404toArray(): Array<T> 405 406获取包含此容器中所有对象的数组。 407 408**系统能力:** SystemCapability.Utils.Lang 409 410**返回值:** 411 412| 类型 | 说明 | 413| -------- | -------- | 414| Array<T> | 返回对应数组。 | 415 416**示例:** 417 418```ts 419let lightWeightSet = new LightWeightSet(); 420lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 421lightWeightSet.add("sdfs"); 422let result = lightWeightSet.toArray(); 423``` 424 425 426### values 427 428values(): IterableIterator<T> 429 430返回包含此映射中包含的键值的新迭代器对象。 431 432**系统能力:** SystemCapability.Utils.Lang 433 434**返回值:** 435 436| 类型 | 说明 | 437| -------- | -------- | 438| IterableIterator<T> | 返回一个迭代器。 | 439 440**示例:** 441 442```ts 443let lightWeightSet = new LightWeightSet(); 444lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 445lightWeightSet.add("sdfs"); 446let iter = lightWeightSet.values(); 447let index = 0; 448while(index < lightWeightSet.length) { 449 console.log(JSON.stringify(iter.next().value)); 450 index++; 451} 452``` 453 454 455### forEach 456 457forEach(callbackfn: (value?: T, key?: T, set?: LightWeightSet<T>) => void, thisArg?: Object): void 458 459通过回调函数来遍历LightWeightSet实例对象上的元素以及元素对应的下标。 460 461**系统能力:** SystemCapability.Utils.Lang 462 463**参数:** 464 465| 参数名 | 类型 | 必填 | 说明 | 466| -------- | -------- | -------- | -------- | 467| callbackfn | function | 是 | 回调函数。 | 468| thisArg | Object | 否 | callbackfn被调用时用作this值。 | 469 470callbackfn的参数说明: 471| 参数名 | 类型 | 必填 | 说明 | 472| -------- | -------- | -------- | -------- | 473| value | T | 否 | 当前遍历到的元素。 | 474| key | T | 否 | 当前遍历到的元素(和value相同)。 | 475| set | LightWeightSet<T> | 否 | 当前调用forEach方法的实例对象。 | 476 477**示例:** 478 479```ts 480let lightWeightSet = new LightWeightSet(); 481lightWeightSet.add("sdfs"); 482lightWeightSet.add("dfsghsf"); 483lightWeightSet.forEach((value, key) => { 484 console.log("value:" + value, "key:" + key); 485}); 486``` 487 488 489### entries 490 491entries(): IterableIterator<[T, T]> 492 493返回包含此映射中包含的键值对的新迭代器对象。 494 495**系统能力:** SystemCapability.Utils.Lang 496 497**返回值:** 498 499| 类型 | 说明 | 500| -------- | -------- | 501| IterableIterator<[T, T]> | 返回一个迭代器。 | 502 503**示例:** 504 505```ts 506let lightWeightSet = new LightWeightSet(); 507lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 508lightWeightSet.add("sdfs"); 509let iter = lightWeightSet.entries(); 510let index = 0; 511while(index < lightWeightSet.length) { 512 console.log(JSON.stringify(iter.next().value)); 513 index++; 514} 515``` 516 517 518### [Symbol.iterator] 519 520[Symbol.iterator]\(): IterableIterator<T> 521 522返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 523 524**系统能力:** SystemCapability.Utils.Lang 525 526**返回值:** 527 528| 类型 | 说明 | 529| -------- | -------- | 530| IterableIterator<T> | 返回一个迭代器。 | 531 532**示例:** 533 534```ts 535let lightWeightSet = new LightWeightSet(); 536lightWeightSet.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf"); 537lightWeightSet.add("sdfs"); 538 539// 使用方法一: 540for (let item of lightWeightSet) { 541 console.log("value:" + item); 542} 543 544// 使用方法二: 545let iter = lightWeightSet[Symbol.iterator](); 546let temp = iter.next().value; 547while(temp != undefined) { 548 console.log("value:" + temp); 549 temp = iter.next().value; 550} 551```