亲宝软件园·资讯

展开

Kotlin Lambda表达式实践使用介绍

z啵唧啵唧 人气:0

集合的创建和遍历方式

val list = ArrayList<String>()
list.add("苹果")
list.add("香蕉")
list.add("橘子")

这种初始化集合的方式比较繁琐,在Kotlin当中给我们提供了一个listOf方法可以简化集合的初始化

val list = listOf<String>("苹果", "香蕉", "橘子")

使用Kotlin当中的for-in方法对集合进行遍历

fun main() {
    val list = listOf<String>("苹果", "香蕉", "橘子")
    for (fruit in list) {
        println(fruit)
    }
}

需要注意的是使用listOf方法初始化的集合是一种不可变的集合,意思就是使用listOf初始化的集合只能用来读取不能用来添加,修改或者删除

那么我们要是想要快速初始化一个集合,又想要可变可以使用mutableListOf方法进行初始化

fun main() {
    val list = mutableListOf<String>("苹果", "香蕉", "橘子")
    list.add("哈密瓜")
    for (fruit in list) {
        println(fruit)
    }
}

Set集合地用户几乎和List一样,只是将创建集合地方式换成了setOf和mutableSetOf

val set = setOf<String>("香蕉", "橘子", "苹果")

set集合存放的元素是不可重复的

Map集合的用法在Kotlin当中也比较多,和java比较类似的是下面这一种

 val map = HashMap<String, Int>()
 map.put("香蕉", 1)
 map.put("西瓜", 2)

但是在Kotlin当中还有一种类似于数组下标的写法

val map = HashMap<String, Int>()
map["香蕉"] = 1
map["西瓜"] = 2

而在map中读取一条写法

map.get("香蕉")
//或者还可以是这样(推荐)
map["香蕉"]

当然上面这些都不是最简单的写法,在Kotlin当中给Map也提供了mapOf和mutableMapOf两个函数进行快速的初始化

fun main() {
    val map = mapOf<String, Int>("香蕉" to 1, "西瓜" to 2)
    for ((fruit, number) in map) {
        println("fruit is " + fruit + "number is " + number)
    }
}

集合的函数式API

在一个水果集合当中,找到单词最长那个水果

写法一:

 val list = listOf<String>("Apple", "Banana", "Orange", "Pear")
 var maxLengthFruit = ""
 for (fruit in list) {
     if (fruit.length > maxLengthFruit.length) {
         maxLengthFruit = fruit
     }
 }
 println("max length fruit is " + maxLengthFruit)

写法二:(使用函数式API写法)

    val list = listOf<String>("Apple", "Banana", "Orange", "Pear")
    val maxLengthFruit = list.maxBy { it.length }
    println("max length fruit is " + maxLengthFruit)

{参数名1: 参数类型, 参数名2: 参数类型 -> 函数体}

    val list = listOf<String>("Apple", "Banana", "Orange", "Pear")
    //定义一个lambda表达式
    val lambda = {fruit: String -> fruit.length}
    //然后将这个lambda表达式作为参数传递给maxBy函数,maxBy函数的作用就是遍历集合,根据参数条件来找到最大值
    val maxLengthFruit = list.maxBy(lambda)
val maxLengthFruit = list.maxBy({<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->fruit: String -> fruit.length})
val maxLengthFruit = list.maxBy(){<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->fruit: String -> fruit.length}
val maxLengthFruit = list.maxBy{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->fruit: String -> fruit.length}
val maxLengthFruit = list.maxBy{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->fruit -> fruit.length}
val maxLenght = list.maxBy{<!--{cke_protected}{C}%3C!%2D%2D%20%2D%2D%3E-->it.length}
    val list = listOf<String>("Apple", "Banana")
    val newList = list.map { it.toUpperCase() }
    for (fruit in newList) {
        println(fruit)
    }
val list = listOf<String>("Apple", "Banana", "Orange", "Pear", "Grape")
val newList = list.filter { it.length <= 5 }
    .map { it.toUpperCase() }
for (fruit in newList) {
    println(fruit)
}
val list = listOf<String>("Apple", "Banana", "Orange", "Pear", "Grape")
val anyResult = list.any {it.length <= 5}
val allResult = list.all { it.length <= 5 }
println("anyResult is" + anyResult + ", allResult is" + allResult)

加载全部内容

相关教程
猜你喜欢
用户评论