前言
在上篇文章中我们学习了lambda的语法规则及简单使用,接下来我们学习下lambda在集合中的具体应用,以及一些库函数的使用。
过滤函数
filter()
,接收一个Boolean类型返回值的lambda表达式
作为参数,收集满足判断条件的所有元素到新集合中并返回。
如果要使用否定条件来过滤集合,可以使用filterNot()
。
private val list = listOf("one", "two", "three")
@JvmStatic
fun main(args: Array<String>) {
val filter = list.filter {
it.length > 3
}
val filterNot = list.filterNot {
it.length > 3
}
println("filter: $filter")
println("filterNot: $filterNot")
}
输出结果:
filter: [three]
filterNot: [one, two]
转换函数
map()
,把每个元素应用到给定的lambda表达式
(返回值为任意类型),把结果收集到新集合中并返回。
private val list = listOf("one", "two", "three")
private val people = listOf(Person("张三","24"), Person("李四","28"), Person("王五","32"))
@JvmStatic
fun main(args: Array<String>) {
val mapOne = list.map { it.length > 3 }
val mapTwo = people .map { it.name }
println("mapOne: $mapOne")
println("mapTwo: $mapTwo")
}
输出结果:
mapOne: [false, false, true]
mapTwo: ["张三", "李四", "王五"]
条件判断函数
all()
,判断集合中的所有元素是否都符合条件。
any()
,判断集合中的是否有元素符合条件。
private val list = listOf("one", "two", "three")
@JvmStatic
fun main(args: Array<String>) {
val all = list.all { it.length > 3 }
val any = list.any { it.length > 3 }
println("all: $all")
println("any: $any")
}
输出结果:
all: false
any: true
查询函数
find()
,找到满足条件的元素并返回其中第一个元素
private val list = listOf("one", "two", "three")
@JvmStatic
fun main(args: Array<String>) {
val find = list.find { it.firstChar() == 't' }
println("find: $find")
}
输出结果:
find: two
分组函数
groupBy()
,把集合按照条件分组,返回一个map
,lambda表达式的结果作为key
,符合条件的存储在同一个集合中作为value。
private val list = listOf("one", "two", "three")
@JvmStatic
fun main(args: Array<String>) {
val groupBy = list.groupBy {
it.firstChar() == 't'
}
val groupByTwo = list.groupBy {
it.firstChar()
}
println("groupBy: $groupBy")
println("groupByTwo: $groupByTwo")
}
输出结果:
groupBy: {false=[one], true=[two, three]}
groupByTwo: {o=[one], t=[two, three]}
平铺函数
flatMap()
,多用于嵌套的集合,先做和map()
一样的操作,对元素进行变换,然后把变换后得到的元素进行平铺,存储到新的集合中。
private val list = listOf("one", "two", "three")
private val listTwo = listOf(Book("Xx", listOf("Tom", "Bob")), Book("Yy", listOf("Mary")))
@JvmStatic
fun main(args: Array<String>) {
val flatMap = list.flatMap { it.toList() }
val flatMapTwo = listTwo.flatMap { it.authors }
println("flatMap: $flatMap")
println("flatMapTwo: $flatMapTwo")
}
class Book(val title: String, val authors: List<String>)
输出结果:
flatMap: [o, n, e, t, w, o, t, h, r, e, e]
flatMapTwo: [Tom, Bob, Mary]
flatten()
,当我们不需要任何变换,只需要平铺时,使用flatten()
private val list = listOf(listOf("one", "two", "three"), listOf("1", "2", "3"))
@JvmStatic
fun main(args: Array<String>) {
println("flatten: ${list.flatten()}")
}
输出结果:one two three 1 2 3
循环函数
forEach()
,当我们遍历不需要下标时,可以用forEach()
来替代for(element in data)
,需要下标时还是需要使用for ((element , index) in data.withIndex())
。
上面就是一些我们项目中会经常使用到的一些集合相关的库函数了,对于其余一些不常用的函数,可以在我们使用到时再去了解,我们就不一一介绍了,我们这里说到的一些函数比如flat
map
flatMap
等,我们发现他们都会生成一个新的集合用来存储操作后的元素,那如果我们链式调用这些集合函数的话,比如list.flat{ it.length > 3 }.map{ it.firstChar()}
,我们就会在flat
的时候创建一个中间集合,即使这个集合我们在最后是用不到的,这无形中就有了无必要的内存消耗,那我们有什么办法可以不创建这个中间集合吗?这就是我们下篇文章说到的序列
了,点波关注,和我一起学习序列
的使用吧~
网友评论