1# @ohos.util.LightWeightSet (非线性容器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文档中存在泛型的使用,涉及以下泛型标记符:<br> 17- T: Type, 类 18 19## 导入模块 20 21```ts 22import LightWeightSet from '@ohos.util.LightWeightSet'; 23``` 24 25## LightWeightSet 26 27### 属性 28 29**系统能力:** SystemCapability.Utils.Lang 30 31| 名称 | 类型 | 可读 | 可写 | 说明 | 32| -------- | -------- | -------- | -------- | -------- | 33| length | number | 是 | 否 | LightWeightSet的元素个数。 | 34 35 36### constructor 37 38constructor() 39 40LightWeightSet的构造函数。 41 42**系统能力:** SystemCapability.Utils.Lang 43 44**错误码:** 45 46以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 47 48| 错误码ID | 错误信息 | 49| -------- | -------- | 50| 10200012 | The LightWeightSet's constructor cannot be directly invoked. | 51 52**示例:** 53 54```ts 55let lightWeightSet = new LightWeightSet(); 56``` 57 58 59### isEmpty 60 61isEmpty(): boolean 62 63判断该容器是否为空。 64 65**系统能力:** SystemCapability.Utils.Lang 66 67**返回值:** 68 69| 类型 | 说明 | 70| -------- | -------- | 71| boolean | 为空返回true,不为空返回false。 | 72 73**错误码:** 74 75以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 76 77| 错误码ID | 错误信息 | 78| -------- | -------- | 79| 10200011 | The isEmpty method cannot be bound. | 80 81**示例:** 82 83```ts 84const lightWeightSet = new LightWeightSet(); 85let result = lightWeightSet.isEmpty(); 86``` 87 88### add 89 90add(obj: T): boolean 91 92向此容器中添加数据。 93 94**系统能力:** SystemCapability.Utils.Lang 95 96**参数:** 97 98| 参数名 | 类型 | 必填 | 说明 | 99| -------- | -------- | -------- | -------- | 100| obj | T | 是 | 添加的成员数据。 | 101 102**返回值:** 103 104| 类型 | 说明 | 105| -------- | -------- | 106| boolean | 成功添加元素返回true,否则返回false。 | 107 108**错误码:** 109 110以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 111 112| 错误码ID | 错误信息 | 113| -------- | -------- | 114| 10200011 | The add method cannot be bound. | 115 116**示例:** 117 118```ts 119let lightWeightSet = new LightWeightSet(); 120let result = lightWeightSet.add("squirrel"); 121``` 122 123 124### addAll 125 126addAll(set: LightWeightSet<T>): boolean 127 128将另一个容器中的所有元素组添加到当前容器中。 129 130**系统能力:** SystemCapability.Utils.Lang 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| -------- | -------- | -------- | -------- | 136| set | LightWeightSet<T> | 是 | 提供添加元素的lightWeightSet。 | 137 138**错误码:** 139 140以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 141 142| 错误码ID | 错误信息 | 143| -------- | -------- | 144| 10200011 | The addAll method cannot be bound. | 145 146**示例:** 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 162判断此容器中是否含有该指定set中的所有元素。 163 164**系统能力:** SystemCapability.Utils.Lang 165 166**参数:** 167 168| 参数名 | 类型 | 必填 | 说明 | 169| -------- | -------- | -------- | -------- | 170| set | LightWeightSet<T> | 是 | 比较对象。 | 171 172**返回值:** 173 174| 类型 | 说明 | 175| -------- | -------- | 176| boolean | 包含所有元素返回true,否则返回false。 | 177 178**错误码:** 179 180以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 181 182| 错误码ID | 错误信息 | 183| -------- | -------- | 184| 10200011 | The hasAll method cannot be bound. | 185 186**示例:** 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 202判断此容器中是否含有该指定key。 203 204**系统能力:** SystemCapability.Utils.Lang 205 206**参数:** 207 208| 参数名 | 类型 | 必填 | 说明 | 209| -------- | -------- | -------- | -------- | 210| key | T | 是 | 指定key | 211 212**返回值:** 213 214| 类型 | 说明 | 215| -------- | -------- | 216| boolean | 包含指定key返回true,否则返回false。 | 217 218**错误码:** 219 220以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 221 222| 错误码ID | 错误信息 | 223| -------- | -------- | 224| 10200011 | The has method cannot be bound. | 225 226**示例:** 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 240判断此容器中是否含有该指定obj同类型的对象。 241 242**系统能力:** SystemCapability.Utils.Lang 243 244**参数:** 245 246| 参数名 | 类型 | 必填 | 说明 | 247| -------- | -------- | -------- | -------- | 248| obj | Object | 是 | 比较对象。 | 249 250**返回值:** 251 252| 类型 | 说明 | 253| -------- | -------- | 254| boolean | 构成类型相同返回true,否则返回false。 | 255 256**错误码:** 257 258以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 259 260| 错误码ID | 错误信息 | 261| -------- | -------- | 262| 10200011 | The equal method cannot be bound. | 263 264**示例:** 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 279将当前容器扩容至可以容纳指定数量元素。 280 281**系统能力:** SystemCapability.Utils.Lang 282 283**参数:** 284 285| 参数名 | 类型 | 必填 | 说明 | 286| -------- | -------- | -------- | -------- | 287| minimumCapacity | number | 是 | 需要容纳数量。 | 288 289**错误码:** 290 291以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 292 293| 错误码ID | 错误信息 | 294| -------- | -------- | 295| 10200011 | The increaseCapacityTo method cannot be bound. | 296| 10200001 | The value of minimumCapacity is out of range. | 297 298**示例:** 299 300```ts 301let lightWeightSet = new LightWeightSet(); 302lightWeightSet.increaseCapacityTo(10); 303``` 304 305 306### getIndexOf 307 308getIndexOf(key: T): number 309 310获取指定key所对应的下标。 311 312**系统能力:** SystemCapability.Utils.Lang 313 314**参数:** 315 316| 参数名 | 类型 | 必填 | 说明 | 317| -------- | -------- | -------- | -------- | 318| key | T | 是 | 查找的指定key。 | 319 320**返回值:** 321 322| 类型 | 说明 | 323| -------- | -------- | 324| number | 在lightWeightSet中指定数据的下标。 | 325 326**错误码:** 327 328以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 329 330| 错误码ID | 错误信息 | 331| -------- | -------- | 332| 10200011 | The getIndexOf method cannot be bound. | 333 334**示例:** 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 348删除并返回指定key对应的元素。 349 350**系统能力:** SystemCapability.Utils.Lang 351 352**参数:** 353 354| 参数名 | 类型 | 必填 | 说明 | 355| -------- | -------- | -------- | -------- | 356| key | T | 是 | 指定key。 | 357 358**返回值:** 359 360| 类型 | 说明 | 361| -------- | -------- | 362| T | 返回删除元素的值。 | 363 364**错误码:** 365 366以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 367 368| 错误码ID | 错误信息 | 369| -------- | -------- | 370| 10200011 | The remove method cannot be bound. | 371 372**示例:** 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 386删除指定下标所对应的元素。 387 388**系统能力:** SystemCapability.Utils.Lang 389 390**参数:** 391 392| 参数名 | 类型 | 必填 | 说明 | 393| -------- | -------- | -------- | -------- | 394| index | number | 是 | 指定下标。 | 395 396**返回值:** 397 398| 类型 | 说明 | 399| -------- | -------- | 400| boolean | 确认是否成功删除元素 | 401 402**错误码:** 403 404以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 405 406| 错误码ID | 错误信息 | 407| -------- | -------- | 408| 10200011 | The removeAt method cannot be bound. | 409 410**示例:** 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 424获取此容器中指定下标对应的元素。 425 426**系统能力:** SystemCapability.Utils.Lang 427 428**参数:** 429 430| 参数名 | 类型 | 必填 | 说明 | 431| -------- | -------- | -------- | -------- | 432| index | number | 是 | 指定下标。 | 433 434**返回值:** 435 436| 类型 | 说明 | 437| -------- | -------- | 438| T | 返回指定下标对应的元素。 | 439 440**错误码:** 441 442以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 443 444| 错误码ID | 错误信息 | 445| -------- | -------- | 446| 10200011 | The getValueAt method cannot be bound. | 447 448**参数:** 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 462清除容器中的所有元素,并把length置为0。 463 464**系统能力:** SystemCapability.Utils.Lang 465 466**错误码:** 467 468以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 469 470| 错误码ID | 错误信息 | 471| -------- | -------- | 472| 10200011 | The clear method cannot be bound. | 473 474**示例:** 475 476```ts 477let lightWeightSet = new LightWeightSet(); 478lightWeightSet.add("squirrel"); 479lightWeightSet.add("sparrow"); 480lightWeightSet.clear(); 481``` 482 483 484### toString 485 486toString(): String 487 488获取包含容器中所有键和值的字符串。 489 490**系统能力:** SystemCapability.Utils.Lang 491 492**返回值:** 493 494| 类型 | 说明 | 495| -------- | -------- | 496| String | 返回对应字符串。 | 497 498**示例:** 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 512获取包含此容器中所有对象的数组。 513 514**系统能力:** SystemCapability.Utils.Lang 515 516**返回值:** 517 518| 类型 | 说明 | 519| -------- | -------- | 520| Array<T> | 返回对应数组。 | 521 522**错误码:** 523 524以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 525 526| 错误码ID | 错误信息 | 527| -------- | -------- | 528| 10200011 | The toArray method cannot be bound. | 529 530**示例:** 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 544返回包含此映射中包含的键值的新迭代器对象。 545 546**系统能力:** SystemCapability.Utils.Lang 547 548**返回值:** 549 550| 类型 | 说明 | 551| -------- | -------- | 552| IterableIterator<T> | 返回一个迭代器。 | 553 554**错误码:** 555 556以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 557 558| 错误码ID | 错误信息 | 559| -------- | -------- | 560| 10200011 | The values method cannot be bound. | 561 562**示例:** 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<T>) => void, thisArg?: Object): void 580 581通过回调函数来遍历LightWeightSet实例对象上的元素以及元素对应的下标。 582 583**系统能力:** SystemCapability.Utils.Lang 584 585**参数:** 586 587| 参数名 | 类型 | 必填 | 说明 | 588| -------- | -------- | -------- | -------- | 589| callbackFn | function | 是 | 回调函数。 | 590| thisArg | Object | 否 | callbackfn被调用时用作this值。 | 591 592callbackfn的参数说明: 593| 参数名 | 类型 | 必填 | 说明 | 594| -------- | -------- | -------- | -------- | 595| value | T | 否 | 当前遍历到的元素。 | 596| key | T | 否 | 当前遍历到的元素(和value相同)。 | 597| set | LightWeightSet<T> | 否 | 当前调用forEach方法的实例对象。 | 598 599**错误码:** 600 601以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 602 603| 错误码ID | 错误信息 | 604| -------- | -------- | 605| 10200011 | The forEach method cannot be bound. | 606 607**示例:** 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 623返回包含此映射中包含的键值对的新迭代器对象。 624 625**系统能力:** SystemCapability.Utils.Lang 626 627**返回值:** 628 629| 类型 | 说明 | 630| -------- | -------- | 631| IterableIterator<[T, T]> | 返回一个迭代器。 | 632 633**错误码:** 634 635以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 636 637| 错误码ID | 错误信息 | 638| -------- | -------- | 639| 10200011 | The entries method cannot be bound. | 640 641**示例:** 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<T> 659 660返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 661 662**系统能力:** SystemCapability.Utils.Lang 663 664**返回值:** 665 666| 类型 | 说明 | 667| -------- | -------- | 668| IterableIterator<T> | 返回一个迭代器。 | 669 670**错误码:** 671 672以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 673 674| 错误码ID | 错误信息 | 675| -------- | -------- | 676| 10200011 | The Symbol.iterator method cannot be bound. | 677 678**示例:** 679 680```ts 681let lightWeightSet = new LightWeightSet(); 682lightWeightSet.add("squirrel"); 683lightWeightSet.add("sparrow"); 684 685// 使用方法一: 686for (let item of lightWeightSet) { 687 console.log("value:" + item); 688} 689 690// 使用方法二: 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```