1# @ohos.util.HashSet (非线性容器HashSet) 2 3>  **说明:** 4> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 5 6HashSet基于[HashMap](js-apis-hashmap.md)实现。在HashSet中,只对value对象进行处理。 7 8HashSet和[TreeSet](js-apis-treeset.md)相比,HashSet中的数据无序存放,即存放元素的顺序和取出的顺序不一致,而TreeSet是有序存放。它们集合中的元素都不允许重复,但HashSet允许放入null值,TreeSet不允许。 9 10**推荐使用场景:** 可以利用HashSet不重复的特性,当需要不重复的集合或需要去重某个集合的时候使用。 11 12文档中存在泛型的使用,涉及以下泛型标记符:<br> 13- T: Type, 类 14 15## 导入模块 16 17```ts 18import HashSet from '@ohos.util.HashSet'; 19``` 20 21## HashSet 22 23### 属性 24 25**系统能力:** SystemCapability.Utils.Lang 26 27| 名称 | 类型 | 可读 | 可写 | 说明 | 28| -------- | -------- | -------- | -------- | -------- | 29| length | number | 是 | 否 | HashSet的元素个数。 | 30 31**示例:** 32 33```ts 34let hashSet = new HashSet(); 35hashSet.add(1); 36hashSet.add(2); 37hashSet.add(3); 38hashSet.add(4); 39hashSet.add(5); 40let res = hashSet.length; 41``` 42 43### constructor 44 45constructor() 46 47HashSet的构造函数。 48 49**系统能力:** SystemCapability.Utils.Lang 50 51**错误码:** 52 53以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 54 55| 错误码ID | 错误信息 | 56| -------- | -------- | 57| 10200012 | The HashSet's constructor cannot be directly invoked. | 58 59**示例:** 60 61```ts 62let hashSet = new HashSet(); 63``` 64 65 66### isEmpty 67 68isEmpty(): boolean 69 70判断该HashSet是否为空。 71 72**系统能力:** SystemCapability.Utils.Lang 73 74**返回值:** 75 76| 类型 | 说明 | 77| -------- | -------- | 78| boolean | 为空返回true,不为空返回false。 | 79 80**错误码:** 81 82以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 83 84| 错误码ID | 错误信息 | 85| -------- | -------- | 86| 10200011 | The isEmpty method cannot be bound. | 87 88**示例:** 89 90```ts 91const hashSet = new HashSet(); 92let result = hashSet.isEmpty(); 93``` 94 95 96### has 97 98has(value: T): boolean 99 100判断此HashSet中是否含有该指定元素。 101 102**系统能力:** SystemCapability.Utils.Lang 103 104**参数:** 105 106| 参数名 | 类型 | 必填 | 说明 | 107| -------- | -------- | -------- | -------- | 108| value | T | 是 | 指定元素。 | 109 110**返回值:** 111 112| 类型 | 说明 | 113| -------- | -------- | 114| boolean | 包含指定元素返回true,否则返回false。 | 115 116**错误码:** 117 118以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 119 120| 错误码ID | 错误信息 | 121| -------- | -------- | 122| 10200011 | The has method cannot be bound. | 123 124**示例:** 125 126```ts 127let hashSet = new HashSet(); 128let result = hashSet.has("squirrel"); 129hashSet.add("squirrel"); 130let result1 = hashSet.has("squirrel"); 131``` 132 133 134### add 135 136add(value: T): boolean 137 138向HashSet中添加数据。 139 140**系统能力:** SystemCapability.Utils.Lang 141 142**参数:** 143 144| 参数名 | 类型 | 必填 | 说明 | 145| -------- | -------- | -------- | -------- | 146| value | T | 是 | 添加成员数据。 | 147 148**返回值:** 149 150| 类型 | 说明 | 151| -------- | -------- | 152| boolean | 成功增加元素返回true,否则返回false。 | 153 154**错误码:** 155 156以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 157 158| 错误码ID | 错误信息 | 159| -------- | -------- | 160| 10200011 | The add method cannot be bound. | 161 162**示例:** 163 164```ts 165let hashSet = new HashSet(); 166let result = hashSet.add("squirrel"); 167``` 168 169 170### remove 171 172remove(value: T): boolean 173 174删除指定的元素。 175 176**系统能力:** SystemCapability.Utils.Lang 177 178**参数:** 179 180| 参数名 | 类型 | 必填 | 说明 | 181| -------- | -------- | -------- | -------- | 182| value | T | 是 | 指定删除的元素。 | 183 184**返回值:** 185 186| 类型 | 说明 | 187| -------- | -------- | 188| boolean | 成功删除指定元素返回true,否则返回false。 | 189 190**错误码:** 191 192以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 193 194| 错误码ID | 错误信息 | 195| -------- | -------- | 196| 10200011 | The remove method cannot be bound. | 197 198**示例:** 199 200```ts 201let hashSet = new HashSet(); 202hashSet.add("squirrel"); 203hashSet.add("sparrow"); 204let result = hashSet.remove("sparrow"); 205``` 206 207 208### clear 209 210clear(): void 211 212清除HashSet中的所有元素,并把length置为0。 213 214**系统能力:** SystemCapability.Utils.Lang 215 216**错误码:** 217 218以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 219 220| 错误码ID | 错误信息 | 221| -------- | -------- | 222| 10200011 | The clear method cannot be bound. | 223 224**示例:** 225 226```ts 227let hashSet = new HashSet(); 228hashSet.add("squirrel"); 229hashSet.add("sparrow"); 230hashSet.clear(); 231``` 232 233 234### values 235 236values(): IterableIterator<T> 237 238返回包含此映射中包含的键值的新迭代器对象。 239 240**系统能力:** SystemCapability.Utils.Lang 241 242**返回值:** 243 244| 类型 | 说明 | 245| -------- | -------- | 246| IterableIterator<T> | 返回一个迭代器。 | 247 248**错误码:** 249 250以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 251 252| 错误码ID | 错误信息 | 253| -------- | -------- | 254| 10200011 | The values method cannot be bound. | 255 256**示例:** 257 258```ts 259let hashSet = new HashSet(); 260hashSet.add("squirrel"); 261hashSet.add("sparrow"); 262let iter = hashSet.values(); 263let temp = iter.next().value; 264while(temp != undefined) { 265 console.log("value:" + temp); 266 temp = iter.next().value; 267} 268``` 269 270 271### forEach 272 273forEach(callbackFn: (value?: T, key?: T, set?: HashSet<T>) => void, thisArg?: Object): void 274 275通过回调函数来遍历实例对象上的元素以及元素对应的下标。 276 277**系统能力:** SystemCapability.Utils.Lang 278 279**参数:** 280 281| 参数名 | 类型 | 必填 | 说明 | 282| -------- | -------- | -------- | -------- | 283| callbackFn | function | 是 | 回调函数。 | 284| thisArg | Object | 否 | callbackfn被调用时用作this值。 | 285 286callbackfn的参数说明: 287| 参数名 | 类型 | 必填 | 说明 | 288| -------- | -------- | -------- | -------- | 289| value | T | 否 | 当前遍历到的元素键值对的值。 | 290| key | T | 否 | 当前遍历到的元素键值对的值(和value相同)。 | 291| set | HashSet<T> | 否 | 当前调用forEach方法的实例对象。 | 292 293**错误码:** 294 295以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 296 297| 错误码ID | 错误信息 | 298| -------- | -------- | 299| 10200011 | The forEach method cannot be bound. | 300 301**示例:** 302 303```ts 304let hashSet = new HashSet(); 305hashSet.add("sparrow"); 306hashSet.add("squirrel"); 307hashSet.forEach((value, key) => { 308 console.log("value:" + value, "key:" + key); 309}); 310``` 311 312 313### entries 314entries(): IterableIterator<[T, T]> 315 316返回包含此映射中包含的键值对的新迭代器对象。 317 318**系统能力:** SystemCapability.Utils.Lang 319 320**返回值:** 321 322| 类型 | 说明 | 323| -------- | -------- | 324| IterableIterator<[T, T]> | 返回一个迭代器。 | 325 326**错误码:** 327 328以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 329 330| 错误码ID | 错误信息 | 331| -------- | -------- | 332| 10200011 | The entries method cannot be bound. | 333 334**示例:** 335 336```ts 337let hashSet = new HashSet(); 338hashSet.add("squirrel"); 339hashSet.add("sparrow"); 340let iter = hashSet.entries(); 341let temp = iter.next().value; 342while(temp != undefined) { 343 console.log("key:" + temp[0]); 344 console.log("value:" + temp[1]); 345 temp = iter.next().value; 346} 347``` 348 349 350### [Symbol.iterator] 351 352[Symbol.iterator]\(): IterableIterator<T> 353 354返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 355 356**系统能力:** SystemCapability.Utils.Lang 357 358**返回值:** 359 360| 类型 | 说明 | 361| -------- | -------- | 362| IterableIterator<T> | 返回一个迭代器 | 363 364**错误码:** 365 366以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。 367 368| 错误码ID | 错误信息 | 369| -------- | -------- | 370| 10200011 | The Symbol.iterator method cannot be bound. | 371 372**示例:** 373 374```ts 375let hashSet = new HashSet(); 376hashSet.add("squirrel"); 377hashSet.add("sparrow"); 378 379// 使用方法一: 380for (let item of hashSet) { 381 console.log("value: " + item); 382} 383 384// 使用方法二: 385let iter = hashSet[Symbol.iterator](); 386let temp = iter.next().value; 387while(temp != undefined) { 388 console.log("value: " + temp); 389 temp = iter.next().value; 390} 391```