Kotlin 不同类型的流#1-渴望使用第一项(如果存在)
示例
//Java:
Arrays.asList("a1", "a2", "a3")
.stream()
.findFirst()
.ifPresent(System.out::println);//科特林:
listOf("a1", "a2", "a3").firstOrNull()?.apply(::println)或者,在名为ifPresent的String上创建扩展函数:
//科特林:
inline fun String?.ifPresent(thenDo: (String)->Unit) = this?.apply { thenDo(this) }
//现在使用新的扩展功能:
listOf("a1", "a2", "a3").firstOrNull().ifPresent(::println)另请参见:apply()功能
另请参阅:扩展功能
另请参阅:“ ?.安全呼叫”运算符和一般的可空性:http://stackoverflow.com/questions/34498562/in-kotlin-what-is-the-idiomatic-way-to-deal-with-nullable-values-referenceencing-o/34498563#34498563
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志