Kotlin-Lambda-集合

2021-03-19  本文已影响0人  在下陈小村

1.kotlin中的list,listof创建的是不可变列表,mutableListOf创建的是可变的列表,set和map同理。

      val list= listOf("Apple","banana")
        val mutableList= mutableListOf<String>("Apple","banana")

        for (fruit in list){
            println(fruit)
        }

2.kotlin中的set

         val set= setOf<String>("Apple","banana")
        val mutableSet= mutableSetOf<String>("Apple","banana")

        for (fruit in set){
            println(fruit)
        }

3.kotlin中的Map

val map= mapOf("Apple" to 1,"banana" to 2)
        val mutableMap = mutableMapOf("Apple" to 1,"banana" to 2)

        for ((fruit,number) in map){
            println("fruit is" + fruit+ "num is"+ number)
        }

4.Lambda的形变原理
取出一个水果集合中名称字符串最长的那个,分别使用java的思路和使用Lambda的解题思路

fun maxLength(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        var maxLengthFruit="";
        for (fruit in list){
            if (fruit.length>maxLengthFruit.length){
                maxLengthFruit=fruit
            }
        }
        println("max length fruit is "+maxLengthFruit)
    }

    //通过集合函数式API简化后的代码
    fun maxLength2(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        var maxLengthFruit=list.maxBy { it.length }
        println(maxLengthFruit)
    }

5.Lambda的形变原理

//lambda表达式的转变的原理
    //{参数1:类型,参数2:类型-> 函数体}
    fun maxLengthLv1(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val lambda={fruit:String ->fruit.length}
        val maxLengthFruit=list.maxBy (lambda)
    }
    //lambda作为maxBy的参数
    fun maxLengthLv2(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val maxLengthFruit=list.maxBy ({fruit:String -> fruit.length})
    }
    //当Lambda参数是函数的最后一个参数时,可以将Lambda表达式移动到()外面
    fun maxLengthLv3(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val maxLengthFruit=list.maxBy (){ fruit:String -> fruit.length }
    }
    //当Lambda参数是函数的最后一个参数的话,可以将函数的()也省略
    fun maxLengthLv4(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val maxLengthFruit=list.maxBy { fruit:String -> fruit.length }
    }
    //Kotlin的类推导机制可以省略Lambda的参数类型
    fun maxLengthLv5(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val maxLengthFruit=list.maxBy { fruit-> fruit.length }
    }
    //当Lambda的参数只有一个是可以不用申明变量,直接使用it来代替
    fun maxLengthLv6(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val maxLengthFruit=list.maxBy { it.length }
    }

6.集合常用的几个函数式API
map
遍历list中的每一个元素,并将它转为大写。map返回的是一个集合,对原来集合处理后的集合

fun lamMap(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val newList=list.map { it.toUpperCase() }
        for (fruit in newList){
            println(fruit)
        }
    }

filter
根据Lambda的条件来删选,返回的也是一个集合。

fun lamFilter(){
        val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val newList=list.filter { it.length>5}
        for (fruit in newList){
            println(fruit)
        }
    }

any:集合中是否存在满足lambda表达式的元素,返回的是boolean值
all:集合中所有的元素是否返祖lambda表达式的条件,返回值是Boolean值

val list= listOf("Apple","Banana","Orange","Pear","Grape","Watermelon")
        val anyResult=list.any() { it.length>5}
        val allResult =list.all { it.length>5 }

7.java中常用的函数式AP在Kotlin中的调用

fun thread(){
        Thread(object :Runnable{
            override fun run() {
                TODO("Not yet implemented")
            }
        }).start()
    }

    fun threadLv1(){
        Thread(Runnable { println("fff") })
    }
    //参数列表仅有一个Java单抽象方法接口参数,可以省略接口名
    fun threadLv2(){
        Thread({ println("ffffff")})
    }
    
    fun threadLv3(){
        Thread{ println("fffff")}.start()
    }
button.setOnClickListener{
}
上一篇 下一篇

猜你喜欢

热点阅读