端计算(6)-kotlin(5)

端计算(6)-kotlin(5),第1张

计算(6)-kotlin(5)

kotlin-lambda

package com.example.s1


data class Student(val sId:Int,val sName:String,var sAge:Int)
fun main(){
    val st1=Student(1,"zhangsang",22)
    val st2=Student(2,"lisi",21)
    val st3=Student(3,"lihui",24)
    val st4=Student(4,"zhangan",29)
    val stlst1=listOf(st1 ,st2,st3,st4 )
    for (st in stlst1){
        println(st)
    }
    val oldestStudent1=stlst1.maxByOrNull {stdata:Student->stdata.sAge}
    println(oldestStudent1)
    val oldestStudent2=stlst1.maxByOrNull {it.sAge}
    println(oldestStudent2)
}
Student(sId=1, sName=zhangsang, sAge=22)
Student(sId=2, sName=lisi, sAge=21)
Student(sId=3, sName=lihui, sAge=24)
Student(sId=4, sName=zhangan, sAge=29)
Student(sId=4, sName=zhangan, sAge=29)
Student(sId=4, sName=zhangan, sAge=29)

Process finished with exit code 0

map,filter,any,all

Student(sId=1, sName=zhangsang, sAge=23)
Student(sId=2, sName=lisi, sAge=23)
Student(sId=3, sName=lihui, sAge=23)
Student(sId=4, sName=zhangan, sAge=30)
Student(sId=1, sName=zhangsang, sAge=23)
Student(sId=2, sName=lisi, sAge=23)
Student(sId=3, sName=lihui, sAge=23)
true
false

Process finished with exit code 0
package com.example.s1


data class Student(val sId:Int,val sName:String,var sAge:Int)
fun main(){
    val st1=Student(1,"zhangsang",22)
    val st2=Student(2,"lisi",22)
    val st3=Student(3,"lihui",22)
    val st4=Student(4,"zhangan",29)
    val stlst1=listOf(st1 ,st2,st3,st4 )
    val stlst2=stlst1.map{Student(it.sId,it.sName,it.sAge+1)}
    for (st in stlst2){
        println(st)
    }
    val stlst3=stlst1.filter{it.sAge <25  }.map{Student(it.sId,it.sName,it.sAge+1)}
    for (st in stlst3){
        println(st)
    }
    val anyRs=stlst1.any{it.sAge <25  }
    println(anyRs)
    val allRs=stlst1.all{it.sAge <25  }
    println(allRs)

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5434922.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存