目录
在Kotlin中,常见的List集合类型有以下几种:
以下是创建List集合以及常见操作的示例:
- // 创建一个不可变的只读List
- val list1 = listOf(1, 2, 3)
- println(list1) // 输出:[1, 2, 3]
-
- // 创建一个可变的List
- val list2 = mutableListOf("apple", "banana", "orange")
- println(list2) // 输出:[apple, banana, orange]
-
- // 创建一个可变的ArrayList
- val list3 = arrayListOf(1.2, 3.4, 5.6)
- println(list3) // 输出:[1.2, 3.4, 5.6]
2.常见操作:
- val numbers = listOf(1, 2, 3, 4, 5)
-
- // 访问元素
- val firstNumber = numbers[0]
- println(firstNumber) // 输出:1
-
- // 添加元素
- val newList = numbers.toMutableList()
- newList.add(6)
- println(newList) // 输出:[1, 2, 3, 4, 5, 6]
-
- // 删除元素
- newList.removeAt(2)
- println(newList) // 输出:[1, 2, 4, 5, 6]
-
- // 修改元素
- newList[1] = 10
- println(newList) // 输出:[1, 10, 4, 5, 6]
-
- // 迭代元素
- for (number in numbers) {
- println(number)
- }
-
- // 过滤元素
- val evenNumbers = numbers.filter { it % 2 == 0 }
- println(evenNumbers) // 输出:[2, 4]
3.泛型约束
例如,在创建一个只包含整数的List时,可以指定List的泛型类型为Int:
- val numbers: List<Int> = listOf(1, 2, 3)
-
- val mutableNumbers: MutableList<Int> = mutableListOf(1, 2, 3)
Kotlin中的Set集合是一种不允许重复元素的集合。由于Set集合中的元素是无序的,因此不能通过索引获取元素。
1.创建Set集合:
- val set: Set<Int> = setOf(1, 2, 3)
-
- val mutableSet: MutableSet<Int> = mutableSetOf(1, 2, 3)
2.Set集合的常见操作:
mutableSet.add(4) mutableSet.remove(3) val containsElement = mutableSet.contains(2) val size = mutableSet.size - for (element in mutableSet) {
- println(element)
- }
- ---------------------
- mutableSet.forEach { element ->
- println(element)
- }
在Kotlin中,Map集合用于存储键值对,其中键和值可以是任意类型。以下是创建Map集合和进行常见操作的示例:
创建Map集合:
val map: MapInt> = mapOf("A" to 1, "B" to 2, "C" to 3) val mutableMap: MutableMapInt> = mutableMapOf("A" to 1, "B" to 2, "C" to 3) Map集合的常见操作:
- mutableMap.put("D", 4)
- mutableMap["E"] = 5
mutableMap.remove("C") val containsKey = mutableMap.containsKey("A") val containsValue = mutableMap.containsValue(2) val size = mutableMap.size - val keys = mutableMap.keys
- val values = mutableMap.values
- for ((key, value) in mutableMap) {
- println("Key: $key, Value: $value")
- }
- ---------------------
- mutableMap.forEach { (key, value) ->
- println("Key: $key, Value: $value")
- }
- fun main() {
- val hashMap = HashMap
Int>() -
- // 添加键值对
- hashMap["Alice"] = 25
- hashMap["Bob"] = 30
- hashMap["Charlie"] = 35
-
- // 获取键对应的值
- val age = hashMap["Alice"]
- println(age) // 输出: 25
-
- // 遍历键值对
- for ((name, age) in hashMap) {
- println("$name: $age")
- }
- // 输出:
- // Alice: 25
- // Bob: 30
- // Charlie: 35
-
- // 检查键是否存在
- val containsKey = hashMap.containsKey("Alice")
- println(containsKey) // 输出: true
-
- // 删除指定键值对
- hashMap.remove("Bob")
- println(hashMap) // 输出: {Alice=25, Charlie=35}
-
- // 清空哈希表
- hashMap.clear()
- println(hashMap.isEmpty()) // 输出: true
- }
在Kotlin中,Stream流操作提供了一种功能强大的方式来处理集合数据。可以使用扩展函数和lambda表达式来对集合进行处理。以下是一些常见的Stream流操作方法及其示例:
- val numbers = listOf(1, 2, 3, 4, 5)
- val squaredNumbers = numbers.map { it * it }
- println(squaredNumbers) // 输出:[1, 4, 9, 16, 25]
- val numbers = listOf(1, 2, 3, 4, 5)
- val evenNumbers = numbers.filter { it % 2 == 0 }
- println(evenNumbers) // 输出:[2, 4]
- val numbers = listOf(1, 2, 3, 4, 5)
- val sum = numbers.reduce { acc, num -> acc + num }
- println(sum) // 输出:15
- val numbers = listOf(1, 2, 3, 4, 5)
- numbers.forEach { println(it) }
- val numbers = listOf(4, 2, 5, 1, 3)
- val sortedNumbers = numbers.sorted()
- println(sortedNumbers) // 输出:[1, 2, 3, 4, 5]
- val numbers = listOf(1, 2, 2, 3, 3, 3, 4, 4, 5)
- val distinctNumbers = numbers.distinct()
- println(distinctNumbers) // 输出:[1, 2, 3, 4, 5]
- data class Person(val name: String, val age: Int)
-
- fun main() {
- val people = listOf(
- Person("Alice", 25),
- Person("Bob", 30),
- Person("Charlie", 35),
- Person("David", 40),
- Person("Emma", 45)
- )
-
- val filteredPeople = people.filter { it.age > 30 }
- .sortedByDescending { it.age }
- .map { "${it.name} (${it.age})" }
-
- println(filteredPeople)
- }
在上述案例中,我们有一个包含Person对象的人员列表。我们首先使用filter函数筛选出年龄大于30岁的人员,然后使用sortedByDescending函数按年龄降序排序,最后使用map函数将Person对象转换为格式化的字符串。最终,我们将过滤和格式化后的结果打印出来。
输出结果将是:
[Emma (45), David (40), Charlie (35)]