val listString= listOf<String>("one","two","one")println(listString)输出:[one, two, one]// set集合去重val setString= setOf<String>("one","two","one")println(setString)输出:[one, two]
2、可变集合
val numbers= mutableListOf<Int>(1,2,3,4)numbers.add(6)numbers.removeAt(1)numbers[0]=10println(numbers)输出:[10,3,4,6]// set集合自动过滤重复元素val hello= mutableSetOf<String>("h","e","l","l","o")hello.remove("o")println(hello)输出:[h, e, l]//集合的加减操作:hello+=setOf("0","w","o","r","l","d")输出: [h, e, l,0, w, o, r, d]
3、Map集合
val numberMap= mapOf<String,Int>("a"to1,"b"to2,"c"to3,"d"to4)println("keys:${numberMap.keys}")println("values:${numberMap.values}")if("a"in numberMap.keys){}if(2in numberMap.values){}输出:
keys:[a, b, c, d]
values:[1,2,3,4]
二、集合的排序
1、随机排序
val number2= mutableListOf<Int>(12,25,20,56,28)// 随机排序number2.shuffled()println("随机$number2")输出:随机[12,25,20,56,28]