# Set Set 对象是值的集合,你可以按照插入的顺序迭代它的元素。Set 中的元素只会出现一次,即 Set 中的元素是唯一的。 ## 构造函数 ### new \(values ?: readonly T[] \| null) : Set\;@Constructor(values?) ## 实例属性 ### size ```ts const set1 = new Set(); set1.add(42); set1.add('forty two'); set1.add('forty two'); console.log(set1.size); // expected output: 2 ``` ## 实例方法 ### add(value) ```ts const set1 = new Set(); set1.add(42); set1.add(42); set1.add(13); set1.forEach((item)=>{ console.log(item); // expected output: 42 // expected output: 13 }) ``` ### clear() ```ts const set1 = new Set(); set1.add(1); set1.add('foo'); console.log(set1.size); // expected output: 2 set1.clear(); console.log(set1.size); // expected output: 0 ``` ### delete(value) ```ts const map1 = new Map(); map1.set('bar', 'foo'); console.log(map1.delete('bar')); // expected result: true // (true indicates successful removal) console.log(map1.has('bar')); // expected result: false ``` ### forEach(callbackfn, thisArg?) ```ts const set1 = new Set([42, 13]); set1.forEach((item)=>{ console.log(item); // expected output: 42 // expected output: 13 }) ``` ### forEach(callbackfn, thisArg?) ### forEach(callbackfn, thisArg?) ### has(value) ```ts const set1 = new Set([1, 2, 3, 4, 5]); console.log(set1.has(1)); // expected output: true console.log(set1.has(5)); // expected output: true console.log(set1.has(6)); // expected output: false ``` . ## Android 平台方法 * 目前 Set 类型编译到 kotlin 为 io.dcloud.uts.Set ::: preview > UTS ```ts // 创建Kotlin HashSet let kotlinSet = new kotlin.collections.HashSet() kotlinSet.add("a") kotlinSet.add("b") // 转换为 UTS Set let utsSet = new Set() utsSet.addAll(kotlinSet) console.log(utsSet) // UTS Set 转换为 Kotlin HashSet let nextKotlinSet = new kotlin.collections.HashSet() nextKotlinSet.addAll(utsSet) console.log(nextKotlinSet) ``` > Kotlin ```Kotlin // 创建Kotlin HashSet var kotlinSet = kotlin.collections.HashSet(); kotlinSet.add("a"); kotlinSet.add("b"); // 转换为 UTS Set var utsSet = Set(); utsSet.addAll(kotlinSet); console.log(utsSet, " at pages/index/helloView.uvue:35"); // UTS Set 转换为 Kotlin HashSet var nextKotlinSet = kotlin.collections.HashSet(); nextKotlinSet.addAll(utsSet); console.log(nextKotlinSet, " at pages/index/helloView.uvue:38"); ``` :::