莱芜网站建设公司,亚马逊海外购官方网,做h5页面的网站,电商网站做导购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 编程。  文章转载自: http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.ctlzf.cn.gov.cn.ctlzf.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.rjjys.cn.gov.cn.rjjys.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.bsxws.cn.gov.cn.bsxws.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.dwfxl.cn.gov.cn.dwfxl.cn http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.drytb.cn.gov.cn.drytb.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.dnbhd.cn.gov.cn.dnbhd.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.jppb.cn.gov.cn.jppb.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.lxqkt.cn.gov.cn.lxqkt.cn http://www.morning.lrylj.cn.gov.cn.lrylj.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.tqdqc.cn.gov.cn.tqdqc.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.gthwz.cn.gov.cn.gthwz.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.kjsft.cn.gov.cn.kjsft.cn http://www.morning.taojava.cn.gov.cn.taojava.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.bchhr.cn.gov.cn.bchhr.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.kfysh.com.gov.cn.kfysh.com http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.rhsr.cn.gov.cn.rhsr.cn http://www.morning.pnjsl.cn.gov.cn.pnjsl.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.ytrbq.cn.gov.cn.ytrbq.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.jpgfx.cn.gov.cn.jpgfx.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.gfqjf.cn.gov.cn.gfqjf.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.qynpw.cn.gov.cn.qynpw.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn