espcms易思企业网站管理系,网站的公关和广告活动怎么做,wordpress列出用户名,有个网站经常换域名Lambda 编程
而 Kotlin 从第一个版本开始就支持了 Lambda 编程#xff0c;并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。
2.6.1 集合的创建与遍历
集合的函数式 API 是入门 Lambda 编程的绝佳示例#xff0c;但在开始之前#xff0c;我们…Lambda 编程
而 Kotlin 从第一个版本开始就支持了 Lambda 编程并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。
2.6.1 集合的创建与遍历
集合的函数式 API 是入门 Lambda 编程的绝佳示例但在开始之前我们需要先学习如何创建集合。
创建集合
传统意义上的集合主要包括 List 和 Set以及键值对数据结构 Map。List 和 Set 在 Java 中是接口常见的实现类有 ArrayList、LinkedList、HashSet 等Map 的常见实现类是 HashMap。
使用 Java 创建集合
假设我们要创建一个包含许多学生姓名和年龄的集合。在 Java 中通常会这样做
import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayListStudent students new ArrayList();students.add(new Student(Alice, 20));students.add(new Student(Bob, 21));students.add(new Student(Charlie, 19));for (Student student : students) {System.out.println(student.getName() is student.getAge() years old.);}}
}class Student {private String name;private int age;public Student(String name, int age) {this.name name;this.age age;}public String getName() {return name;}public int getAge() {return age;}
}
这种初始化集合的方式比较繁琐。
使用 Kotlin 创建集合
Kotlin 提供了内置的 listOf() 函数来简化集合的初始化
data class Student(val name: String, val age: Int)fun main() { val students listOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19))
}
只需一行代码即可完成集合的初始化操作。
遍历集合
Kotlin 中可以使用 for-in 循环来遍历集合。以下是一个示例
data class Student(val name: String, val age: Int)fun main() { val students listOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19))for (student in students) { println(${student.name} is ${student.age} years old.)}
}
运行上述代码输出结果如下
Alice is 20 years old.
Bob is 21 years old.
Charlie is 19 years old.
不可变与可变集合
listOf() 函数创建的是一个不可变集合这意味着不能对其进行添加、修改或删除操作。如果需要创建一个可变集合可以使用 mutableListOf() 函数 data class Student(val name: String, val age: Int)fun main() { val students mutableListOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19))students.add(Student(David, 22)) for (student in students) { println(${student.name} is ${student.age} years old.)}
}
运行上述代码输出结果如下
Alice is 20 years old.
Bob is 21 years old.
Charlie is 19 years old.
David is 22 years old.
Set 集合
Set 集合的用法与 List 类似只是将创建集合的方式换成了 setOf() 和 mutableSetOf() 函数
data class Student(val name: String, val age: Int)fun main() { val uniqueStudents setOf(Student(Alice, 20), Student(Bob, 21), Student(Charlie, 19)) for (student in uniqueStudents) { println(${student.name} is ${student.age} years old.)}
}
需要注意Set 集合中不允许存放重复元素。
Map 集合
Map 是一种键值对形式的数据结构。传统的 Map 用法是先创建一个 HashMap 实例然后将一个个键值对添加到 Map 中
import java.util.HashMap;public class Main {public static void main(String[] args) {HashMapString, Integer studentAges new HashMap();studentAges.put(Alice, 20);studentAges.put(Bob, 21);studentAges.put(Charlie, 19);for (String name : studentAges.keySet()) {System.out.println(Name is name , Age is studentAges.get(name));}}
}
在 Kotlin 中建议使用类似于数组下标的语法来操作 Map
fun main() { val studentAges HashMapString, Int()studentAges[Alice] 20studentAges[Bob] 21studentAges[Charlie] 19
}
更简洁的做法是使用 mapOf() 和 mutableMapOf() 函数来创建 Map
fun main() { val studentAges mapOf(Alice to 20, Bob to 21, Charlie to 19)
}
这里的 to 是一个 infix 函数用于关联键值对。
遍历 Map 集合
遍历 Map 集合时仍然可以使用 for-in 循环
fun main() { val studentAges mapOf(Alice to 20, Bob to 21, Charlie to 19)for ((name, age) in studentAges) { println(Name is $name, Age is $age) }
}
运行上述代码输出结果如下
Name is Alice, Age is 20
Name is Bob, Age is 21
Name is Charlie, Age is 19
总结
通过本节的学习我们掌握了 Kotlin 中集合的创建与遍历方法。Kotlin 提供了简洁的语法来初始化和操作集合使得代码更加清晰和易于维护。接下来我们将学习集合的函数式 API从而正式入门 Lambda 编程。