1、定义
集合是可变数量 (可能为零)的一组条目(集合中的对象称为元素或条目),通常包含相同类型(及其子类型)的一些对象
2、集合类型
- List 是一个有序集合,可通过索引(反映元素位置的整数)访问元素;元素有序、可重复
- Set 是唯一元素的集合,一组无重复的对象,元素的顺序并不重要
- Map(或者字典)是一组键值对,键是唯一的,一个键映射一个值,值可以重复
Kotlin 集合接口的图表
3、List
List<T> 以指定的顺序存储元素,并提供使用索引访问元素的方法;索引从 0(第一个元素的索引)开始直到 lastIndex
(即 (list.size - 1)
)
val numbers = listOf("one", "two", "three", "four")
println("Number of elements: ${numbers.size}") // Number of elements: 4
println("Third element: ${numbers.get(2)}") // Third element: three
println("Fourth element: ${numbers[3]}") // Fourth element: four
println("Index of element \"two\" ${numbers.indexOf("two")}") // Index of element "two" 1
4、MutableList
MutableList<T> 是可以进行写操作的 List,即可在特定位置添加或删除元素;(默认实现是 ArrayList,可以将其视为可调整大小的数组)
val numbers = mutableListOf(1, 2, 3, 4)
numbers.add(5)
numbers.removeAt(1)
numbers[0] = 0
numbers.shuffle() // 将此列表中的元素随机打乱
println(numbers) // [4, 3, 0, 5]
5、Set
Set<T> 存储唯一的元素,与顺序无关;一个 Set
只能包含一个 null
;当两个 set
具有相同的大小并且对于一个 set
中的每个元素都能在另一个 set
中存在相同元素,则两个 set
相等
val numbers = setOf(1, 2, 3, 4)
println("Number of elements: ${numbers.size}") // Number of elements: 4
if (numbers.contains(1)) println("1 is in the set") // 1 is in the set
val numbersBackwards = setOf(4, 3, 2, 1)
println("The sets are equal: ${numbers == numbersBackwards}") // The sets are equal: true
6、MutableSet
MutableSet 是一个带有来自 MutableCollection
的写操作接口的 Set
默认实现 - LinkedHashSet—— 保留元素插入的顺序,因此依赖于顺序的函数,例如 first() 或 last(),会在这些 set 上返回可预测的结果
val numbers = setOf(1, 2, 3, 4) // 默认实现 LinkedHashSet
val numbersBackwards = setOf(4, 3, 2, 1)
println(numbers.first() == numbersBackwards.first()) // false
println(numbers.first() == numbersBackwards.last()) // true
另一种实现方式 – HashSet—— 不声明元素的顺序,所以在它上面调用这些函数会返回不可预测的结果;但 HashSet
只需要较少的内存来存储相同数量的元素
7、Map
Map<K, V> 非 Collection
接口的继承者;是 Kotlin 的另一种集合类型;Map
存储 键-值 对(或 条目);键是唯一的,但是不同的键可以与相同的值配对;Map
接口提供特定的函数进行通过键访问值、搜索键和值等操作
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
println("All keys: ${numbersMap.keys}") // All keys: [key1, key2, key3, key4]
println("All values: ${numbersMap.values}") // All values: [1, 2, 3, 1]
if ("key2" in numbersMap) println("Value by key \"key2\": ${numbersMap["key2"]}") // Value by key "key2": 2
if (1 in numbersMap.values) println("The value 1 is in the map") // The value 1 is in the map
if (numbersMap.containsValue(1)) println("The value 1 is in the map") // The value 1 is in the map
无论键值对的顺序如何,包含相同键值对的两个 Map
是相等的
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)
val anotherMap = mapOf("key2" to 2, "key1" to 1, "key4" to 1, "key3" to 3)
println("The maps are equal: ${numbersMap == anotherMap}") // The maps are equal: true
8、MutableMap
MutableMap 是一个具有写操作的 Map
接口,可以使用该接口添加一个新的键值对或更新给定键的值
val numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
numbersMap["one"] = 11
println(numbersMap) // {one=11, two=2, three=3}
默认实现 – LinkedHashMap —— 迭代 Map 时保留元素插入的顺序
另一种实现 – HashMap —— 不声明元素的顺序
9、ArrayDeque
ArrayDeque<T> 是一个双端队列的实现,可在队列的开头或结尾添加或删除元素;使用可调整大小的阵列来实现,在需要时自动调整大小;同时扮演Kotlin中的Stack和Queue数据结构的角色
val deque = ArrayDeque(listOf(1, 2, 3))
deque.addFirst(0)
deque.addLast(4)
println(deque) // [0, 1, 2, 3, 4]
println(deque.first()) // 0
println(deque.last()) // 4
deque.removeFirst()
deque.removeLast()
println(deque) // [1, 2, 3]