distinctBy
所在位置是kotlin.sequences.distinctBy
,其相關用法介紹如下。
用法:
返回一個序列,該序列僅包含給定序列中的元素,這些元素具有給定 selector 函數返回的不同鍵。
在給定序列的具有相同鍵的元素中,隻有第一個元素會出現在結果序列中。結果序列中的元素與它們在源序列中的順序相同。
操作是 intermediate
和 stateful
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'A', 'b', 'B', 'A', 'a')
println(list.distinct()) // [a, A, b, B]
println(list.distinctBy { it.uppercaseChar() }) // [a, b]
//sampleEnd
}
輸出:
[a, A, b, B] [a, b]
相關用法
- Kotlin distinctBy用法及代碼示例
- Kotlin distinct用法及代碼示例
- Kotlin digitToIntOrNull用法及代碼示例
- Kotlin digitToInt用法及代碼示例
- Kotlin digitToChar用法及代碼示例
- Kotlin dropWhile用法及代碼示例
- Kotlin dropLast用法及代碼示例
- Kotlin dropLastWhile用法及代碼示例
- Kotlin decapitalize用法及代碼示例
- Kotlin drop用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin reduceRight用法及代碼示例
- Kotlin Random.Default用法及代碼示例
- Kotlin Byte.inc用法及代碼示例
- Kotlin getValue用法及代碼示例
- Kotlin Double.dec用法及代碼示例
- Kotlin windowedSequence用法及代碼示例
- Kotlin contentToString用法及代碼示例
- Kotlin groupByTo用法及代碼示例
- Kotlin commonPrefixWith用法及代碼示例
- Kotlin MatchResult.Destructured用法及代碼示例
- Kotlin Delegates.notNull用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.sequences.distinctBy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。