Kotlin的一些扩展函数
Kotlin的一些扩展函数
标签(空格分隔): Android Kotlin
StandardKt.class 通用函数
repeat(Int){}
代码块执行多少次
1  | repeat(2){  | 
with(obj){}
用法1:接收一个对象,在代码块中直接调用属性值赋值
用法2:接收一个对象后,可使用其属性转换成另外一个对象返回
1  | with(TextView(context)) {  | 
.run{}
与with类似,只不过是错用在对象上
1  | TextView(context).run {  | 
.let{}
多用于执行依据代码块,用在可能为空的对象上
1  | val data = 1  | 
.apply{}
对象的扩展方法,类似with
1  | val textView = TextView(context).apply {  | 
.takeIf{}
.takeUnless{}
synchronized(Any){}
CloseableKt.class
.use{}
用于流操作,能够自动关闭流资源
1  | assets.open("data.json").reader().use {  | 
closeFinally()
CollectionsKt.class
.groupBy{}
用于对集合进行分组,返回的是Map集合,key是{条件},value是集合中的对象
1  | val list = listOf("abc", "fd", "edd", "zdc","pkj")  | 
.forEach{}
对集合进行遍历,array,list,map…
1  | val list = listOf("abc", "fd", "edd", "zdc","pkj")  | 
.forEachIndexed{ index, T -> }
对数组进行遍历,提供角标和对象
1  | list.forEachIndexed { index, s ->  | 
.filter{}
筛选指定条件的对象返回List
1  | list.filter {  | 
.reversed()
返回集合的倒序
1  | list.reversed()  | 
List.zip(List):List<Pair<T, R>>  
两个集合合并,返回Pair对象
.average()
返回集合内数的平均值
.count()
返回集合的个数
…more collect