Kotlin系列——集合操作

2018-01-18  本文已影响712人  李晓通

前言

今天给大家带来Kotlin系列的第四篇文章,集合操作,在Kotlin中,集合主要有以下几种

演示

给大家带来这一篇集合操作的目的就是让大家写出更加简介的代码,举个例子

比如我们现在有一个集合students,集合里面有10个数据,数据的id是0到9,我们判断其中是否有一个id等于5,如果有就返回true
java代码

for (Student student : students) {
     if (student.getId() == 5){
          return true;
      }
 }
 return false;

kotlin代码

return students.any { it.id == 5 }

看到差别了么,同样的逻辑在kotlin中只需要一行代码。

好戏开场

这里我先定义一个集合,下面会用到

val list = listOf(0, 1, 2, 3, 4, 5, 6, 7)

聚合操作

println(list.any { it % 2 == 1 })
打印结果:true
println(list.all { it % 2 == 1 })
打印结果:false
println(list.none { it % 2 == 10 })
打印结果:true
println(list.count { it % 2 == 1 })
打印结果:4
list.forEach { it -> if (it > 6) println(it) }
打印结果:7
list.forEachIndexed { index, it -> if (it > 6) println("大于6的值是$it索引是$index") }
打印结果:大于6的值是7索引是7
println(list.max())
打印结果:7
println(list.min())
打印结果:0
//比如这里我给的初始值为10
println(list.fold(10) { total, next -> total + next })
打印结果:38
//比如这里我给的初始值为10
println(list.foldRight(10) { total, next -> total + next })
打印结果:38
println(list.reduce { total, next -> total + next })
打印结果:28
println(list.reduceRight { total, next -> total + next })
打印结果:28
println(list.sumBy { it % 2 })
打印结果:4

过滤操作

println(list.drop(4))
//去掉了0,1,2,3这4个元素
打印结果:[4, 5, 6, 7] 
println(list.dropWhile { it < 5 })
打印结果:[5,6,7] 
println(list.dropLastWhile { it < 7 })
//因为7<5不满足,所以直接停止,返回所有集合
打印结果:[0,1,2,3,4,5,6,7] 
println(list.filter { it % 2 == 0 })
打印结果:[0,2,4,6] 
println(list.filterNot { it % 2 == 0 })
打印结果:[1,3,5,7] 
println(list.filterNotNull())
打印结果:[0,1,2,3,4,5,6,7] 
println(list.take(3))
打印结果:[0,1,2] 
println(list. takeLast(3))
打印结果:[5,6,7] 
println(list.takeWhile { it < 3 })
打印结果:[0,1,2] 

映射操作

println(list.flatMap { listOf(it, it + 2) })
打印结果:[0,2,1,3,2,4,3,5,4,6,5,7,6,8,7,9] 
println(list.groupBy { if (it % 2 == 0) "偶数和0" else "奇数" })
打印结果:{偶数和0=[0, 2, 4, 6], 奇数=[1, 3, 5, 7]}
println(list.map { it + 1 })
打印结果:[1,2,3,4,5,6,7,8] 
println(list.mapIndexed { index, it -> index + it })
打印结果:[0,2,4,6,8,10,12,14] 
println(list.mapNotNull { it + 1 })
打印结果:[1,2,3,4,5,6,7,8] 

元素操作

println(list.contains(3))
打印结果:true
println(list.elementAt(1))
打印结果:1
println(list.elementAtOrElse(9, { it }))
打印结果:9
println(list.elementAtOrNull(9))
打印结果:null
println(list.first { it % 2 == 0 })
打印结果:0
println(list.firstOrNull { it % 2 == 100 })
打印结果:null
println(list.last { it % 2 == 0 })
打印结果:6
println(list.lastIndexOf(5))
打印结果:5
println(list.lastOrNull { it % 2 == 100 })
打印结果:null
println(list.indexOf(3))
打印结果:3
println(list.indexOfFirst { it % 2 == 0 })
打印结果:0
println(list.indexOfLast { it % 2 == 0 })
打印结果:6
println(list.single { it % 7 == 2 })
打印结果:2
println(list.singleOrNull { it % 7 == 100 })
打印结果:null

生成操作

println(list.partition { it % 2 == 0 })
打印结果:([0, 2, 4, 6], [1, 3, 5, 7])
println(list.plus(listOf(8,9)))
打印结果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
println(list.zip(listOf(5, 6, 7)))
打印结果:[(0, 5), (1, 6), (2, 7)]

排序操作

println(list.reversed())
打印结果:[7, 6, 5, 4, 3, 2, 1, 0]
println(list.sorted())
打印结果:[0, 1, 2, 3, 4, 5, 6, 7]
println(list.sortedBy {it % 3})
打印结果:[0, 3,6,1,4,7,2,5]
println(list.sortedDescending())
打印结果:[7, 6, 5, 4, 3, 2, 1, 0]
println(list.sortedByDescending{it % 3})
打印结果:[2,5,1,4,7,0,3,6]

总结

以上就是Kotlin语言的集合及相关操作,希望该文能帮助大家更好的使用Kotlin,可以更加快速高效的进行安卓开发。
哇,写了好久终于写完了,看到这里了你还不点个赞么0_0

以上纯属于个人平时工作和学习的一些总结分享,如果有什么错误欢迎随时指出,大家可以讨论一起进步。

上一篇 下一篇

猜你喜欢

热点阅读