Kotlin的Lambda表达式
2018-04-02 本文已影响6人
墙角的牵牛花
最近在学《Kotlin实战》,顺便记录一下Lambda表达式那一章相关知识点
声明对象
class Person (val name:String ,val age:Int)
val people= listOf(Person("ds",12), Person("afdsdf",54))
//println(people.maxBy { it.age }) 这里it也可以使用
println(people.maxBy (Person :: name))
这样打印结果:main.chapter5.Person@2c7b84de
增加申明对象时data
data class Person (val name:String ,val age:Int)
结果:Person(name=ds, age=12)
Lambda也可以这样写: println(people.maxBy { p:Person ->p.age })
或者:
println(people.maxBy({ p: Person -> p.age }))
输出全部名字:
val name=people.joinToString (separator = " ",transform = {p: Person -> p.name})
println(name)
或者:
val name=people.joinToString (separator = " ") { p: Person -> p.name}
Person ::age 在Kotlin中,有个名字:【成员引用】。
关于filter和map
一个简单的例子
val list = listOf(3,5,7,13233)
print(list.filter { it % 3==0 })
println(people.map(Person::name).filter{it.startsWith("d")})
对象filter:
val people= listOf(Person("dfds",12), Person("fd",53))
print(people.filter { it.age>50 })
map 输出所有名字
println(people.map{it.name})
maxBy:按年龄
println(people.filter { it.age== people.maxBy (Person :: age)?.age ?: null })
all,any,count,find,groupBy和java8的都差不多
any:任意一个
val judgeBy20={p : Person -> p.age <= 27}
println(people.any(judgeBy20))
结果:true
换成all之后,结果为false
换成count
结果:1
size写法:
println(people.filter(judgeBy20).size)
find:
println(people.find(judgeBy20))
groupby:
println(people.groupBy{it.age})
字符串分组
val list1= listOf("a","abc","ababa","bdaaandadbb");
println(list1.groupBy (String::first ))
结果:
{a=[a, abc, ababa], b=[bdaaandadbb]}
flatMap和flatten:处理嵌套集合中的元素
data class Book (val name:String,val authors :List<String>)
flatMap:
val books= listOf(Book("a1", listOf("a","aa","aaa")),
Book("b1", listOf("b","bb","bbb")),
Book("c1", listOf("c","cc","ccc"))
)
println(books.flatMap { it.authors })
list集合
//集合转序列
var lists=people.asSequence()
.map(Person::name)
.filter { it.startsWith("d") }
.toList()
for (i in 0 until lists.size){
println(lists[i])
}
序列:0-100求和
val num= generateSequence(0) { it+1 }
val num0=num.takeWhile { it<=100 }
println(num0.sum())
一个demo,打印字母表
fun alphabet():String {
val result=StringBuilder()
for (letter in 'A'..'Z'){
result.append(letter)
}
result.append("fdddfsadasf");
return result.toString()
}
改进1:
fun alphabet1():String {
val result=StringBuilder()
return with(result){
for (letter in 'A'..'Z'){
result.append(letter)
}
result.append("fdddfsadasf");
this.toString()
}
}
改进2:
fun alphabet2()=with(StringBuilder()){
for (letter in 'A'..'Z'){
append(letter)
}
append("fdddfsadasf")
toString()
}
改进3:
fun alphabet3()= buildString{
for (letter in 'A'..'Z'){
append(letter)
}
append("fdddfsadasf")
toString()
}
apply改进:
fun alphabetApply()= StringBuilder().apply{
for (letter in 'A'..'Z'){
append(letter)
}
append("fdddfsadasf")
}.toString()
apply有点类似java中builder。apply时钟返回作为实参传递给的对象。接受者就是对象。
filter函数demo
fun String.filter(predicate: (Char) -> Boolean):String {
val sb=StringBuilder()
for (index in 0 until length) {
val element=get(index)
if (predicate(element)) sb.append(element)
}
return sb.toString()
}
println("ddfjhhk345576878dsfsdf".filter { it in 'a'.. 'z' })
函数类
fun processTheAnswer(f:(Int) -> Int){
println(f(42))
}
java中可以直接调用