Kotlin学习--集合操作符之总数操作符

来源:互联网 发布:巴塞罗那进球数据 编辑:程序博客网 时间:2024/06/11 04:32

原集合 val list_num : ArrayList = arrayListOf(1,2,3,5,6,7,8)
1.any(只要有一个符合就返回true)
操作符:
val any = list_num.any { it > 5 }
结果:
any–>true
2.all(集合中所有元素都满足函数才返回true)
操作符:
val all = list_num.all { it >0 }
结果:
all–>true
操作符:
val all = list_num.all { it >3 }
结果:
all–>false
3.count(统计集合中满足条件的元素总数)
操作符:
val count = list_num.count { it > 3 && it < 7 }
结果:
count–>2
4.none(如果没有任何元素与给定的函数匹配,则返回true)
操作符:
val none = list_num.none { it > 1 }
结果:
none–>false
5.foreach(遍历每个元素并且进行操作)
操作符:
list_num.forEach{
Log.e(Str,”it–> $it”)
}
结果:
打印了集合中的所有元素
6.foreachindexed(与foreach相同,但是可以得到index)
操作符:

        list_num.forEachIndexed{index,value -> Log.e(Str,"index---> $index value--->  $value")}

结果:
打印出来是对应下标对应的value值
7.max(min) 获取集合中的最大值(最小值),如果集合中无最大值(最小值)则返回null
8.maxBy(minBy) 根据指定的函数返回最大值 minBy同,同理,如果没有最大值(最小值)返回null
9.sumBy 每项经过函数转换后的和

原创粉丝点击