当前位置: 首页 > news >正文

Android学习(七)-Kotlin编程语言-Lambda 编程

Lambda 编程

而 Kotlin 从第一个版本开始就支持了 Lambda 编程,并且 Kotlin 中的 Lambda 功能极为强大。Lambda 表达式使得代码更加简洁和易读。

2.6.1 集合的创建与遍历

集合的函数式 API 是入门 Lambda 编程的绝佳示例,但在开始之前,我们需要先学习如何创建集合。

创建集合

传统意义上的集合主要包括 List 和 Set,以及键值对数据结构 Map。List 和 Set 在 Java 中是接口,常见的实现类有 ArrayListLinkedListHashSet 等;Map 的常见实现类是 HashMap

使用 Java 创建集合

假设我们要创建一个包含许多学生姓名和年龄的集合。在 Java 中,通常会这样做:

import java.util.ArrayList;public class Main {public static void main(String[] args) {ArrayList<Student> 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) {HashMap<String, 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 = HashMap<String, 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.lryc.cn/news/507369.html

相关文章:

  • 彻底认识和理解探索分布式网络编程中的SSL安全通信机制
  • 【libuv】Fargo信令2:【深入】client为什么收不到服务端响应的ack消息
  • Vue3自定义事件
  • BeautifulSoup 与 XPath 用法详解与对比
  • Emacs 折腾日记(五)——elisp 数字类型
  • 重拾设计模式--外观模式
  • 源码编译llama.cpp for android
  • StarRocks 排查单副本表
  • Windows11 家庭版安装配置 Docker
  • 线程知识总结(二)
  • 解决vscode ssh远程连接服务器一直卡在下载 vscode server问题
  • 【Cadence射频仿真学习笔记】IC设计中电感的分析、建模与绘制(EMX电磁仿真,RFIC-GPT生成无源器件及与cadence的交互)
  • Definition of Done
  • 【漏洞复现】CVE-2023-37461 Arbitrary File Writing
  • 简单工厂模式和策略模式的异同
  • HuggingFace datasets - 下载数据
  • 梯度(Gradient)和 雅各比矩阵(Jacobian Matrix)的区别和联系:中英双语
  • Vscode搭建C语言多文件开发环境
  • 自定义 C++ 编译器的调用与管理
  • 时间管理系统|Java|SSM|JSP|
  • 用SparkSQL和PySpark完成按时间字段顺序将字符串字段中的值组合在一起分组显示
  • Sentinel 学习笔记3-责任链与工作流程
  • Latex 转换为 Word(使用GrindEQ )(英文转中文,毕业论文)
  • 使用Chat-LangChain模块创建一个与用户交流的机器人
  • 国家认可的人工智能从业人员证书如何报考?
  • 【网络云计算】2024第51周-每日【2024/12/17】小测-理论-解析
  • 每日十题八股-2024年12月19日
  • 网络方案设计
  • 学习记录:electron主进程与渲染进程直接的通信示例【开箱即用】
  • 【Java数据结构】ArrayList类