在kotlin中有一些常用的对集合进行操作的函数式API,掌握他们可以大大提高编程效率,这些API与java8中的
Stream API类似。下面主要讲解以下几大类
filter主要用作过滤器(对集合元素),它的返回值也是一个集合,定义如下:
public inline fun Iterable.filter(predicate: (T) -> Boolean): List {
return filterTo(ArrayList(), predicate)
}
可以看到,返回的也是一个集合,并且类型与原类型相同,用法如下:
class User(val id:Int,var name:String,var age:Int,val sex:String){
init {
//println(toString())
}
override fun toString(): String {
return "User(id=$id, name='$name', age=$age, sex='$sex')"
}
}
fun main(args: Array) {
val list = arrayListOf(User(1,"张三",14,"男"),User(2,"李四",16,"女"),
User(3,"王五",65,"男"),User(4,"赵六",24,"男")
,User(5,"向七",89,"女"),User(6,"王八",6,"女")
,User(7,"韩九",27,"男"),User(8,"陈10",35,"女"))
//测试filter
val filterList = list.filter { it.age > 30 }
println(filterList)
}
输出:
[User(id=3, name='王五', age=65, sex='男'), User(id=5, name='向七', age=89, sex='女'), User(id=8, name='陈10', age=35, sex='女')]
map主要对集合的元素做映射,可以映射成不同的类型,例如Int到String,定义如下
public inline fun Iterable.map(transform: (T) -> R): List {
return mapTo(ArrayList(collectionSizeOrDefault(10)), transform)
}
可以看到,返回值是映射成的新类型的集合,用法如下
//测试map
val mapList = list.map { it.name }
println(mapList)
输出
[张三, 李四, 王五, 赵六, 向七, 王八, 韩九, 陈10]
以上4个都是对集合的所有元素检查是否符合某个条件(或者它的变种,是否存在符合的元素等)
all检查集合中是否所有元素都符合某个条件
public inline fun Iterable.all(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return true
for (element in this) if (!predicate(element)) return false
return true
}
可以看到返回值是Boolean类型,都符合返回为true,有一个不符合为true,用法如下:
//测试all
val all = list.all { it.age > 10 }
println(all)
输出:
false
any检测集合中是否至少存在一个元素满足条件,如果是就返回true
public inline fun Iterable.any(predicate: (T) -> Boolean): Boolean {
if (this is Collection && isEmpty()) return false
for (element in this) if (predicate(element)) return true
return false
}
用法如下:
//测试any
val any = list.any { it.age > 10 }
println(any)
输出
true
count 检查集合中有多少个元素满足该条件,返回值是Int
public inline fun Iterable.count(predicate: (T) -> Boolean): Int {
if (this is Collection && isEmpty()) return 0
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
用法如下:
//测试count
val count = list.count { it.age > 25 }
println(count)
输出
4
find找到集合中第一个满足条件的元素,返回值是集合元素类型,可能为null
@kotlin.internal.InlineOnly
public inline fun Iterable.find(predicate: (T) -> Boolean): T? {
return firstOrNull(predicate)
}
用法如下:
//测试find
val find = list.find { it.age > 25 }
println(find)
输出
User(id=3, name='王五', age=65, sex='男')
groupBy主要用作将集合按照给定的条件进行分组,即将集合转为map
public inline fun Iterable.groupBy(keySelector: (T) -> K): Map> {
return groupByTo(LinkedHashMap>(), keySelector)
}
可以看到,返回的是一个map,key类型就是lambda的返回值类型,value是集合元素的List,用法如下:
//测试groupBy
val groupBy = list.groupBy { it.sex}
println(groupBy)
输出
{男=[User(id=1, name='张三', age=14, sex='男'), User(id=3, name='王五', age=65, sex='男'), User(id=4, name='赵六', age=24, sex='男'), User(id=7, name='韩九', age=27, sex='男')], 女=[User(id=2, name='李四', age=16, sex='女'), User(id=5, name='向七', age=89, sex='女'), User(id=6, name='王八', age=6, sex='女'), User(id=8, name='陈10', age=35, sex='女')]}
flatMap先对集合中的某个元素映射成集合,再将所有映射成的集合做一个元素平铺,即做一个list
public inline fun Iterable.flatMap(transform: (T) -> Iterable): List {
return flatMapTo(ArrayList(), transform)
}
返回集合元素映射的集合的list
用法如下:
fun main(args: Array) {
val list = arrayListOf(User(1,"张三",14,"男"),User(2,"李四",16,"女"),
User(3,"王五",65,"男"),User(4,"赵六",24,"男")
,User(5,"向七",89,"女"),User(6,"王八",6,"女")
,User(7,"韩九",27,"男"),User(8,"陈10",35,"女"))
....
var citys = arrayListOf()
citys.add(City("CD", arrayListOf(User(1,"张三",14,"男"),User(2,"李四",16,"女"),
User(3,"王五",65,"男"))))
citys.add(City("CD", arrayListOf(User(5,"向七",89,"女"),User(6,"王八",6,"女"))))
//测试flatMap
val flatmap = citys.flatMap {
it.people
}
println(flatmap)
}
class City(val name:String,val people:List){
override fun toString(): String {
return "City(name='$name', people=$people)"
}
}
输出:
[User(id=1, name='张三', age=14, sex='男'), User(id=2, name='李四', age=16, sex='女'), User(id=3, name='王五', age=65, sex='男'), User(id=5, name='向七', age=89, sex='女'), User(id=6, name='王八', age=6, sex='女')]
以上就是Kotlin中集合常用的一些API,熟练使用这些可以非常方便的提高我们的开发效率