<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 過濾 [TOC] 過濾是最常用的集合處理任務之一。在Kotlin中,過濾條件由 _謂詞_ 定義——接受一個集合元素并且返回布爾值的 lambda 表達式:`true` 說明給定元素與謂詞匹配,`false` 則表示不匹配。 標準庫包含了一組讓你能夠通過單個調用就可以過濾集合的擴展函數。這些函數不會改變原始集合,因此它們[既可用于可變集合也可用于只讀集合](collections-overview.html#集合類型)。為了操作過濾結果,應該在過濾后將其賦值給變量或鏈接其他函數。 ## 按謂詞過濾 基本的過濾函數是 [`filter()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter.html)。<!-- -->當使用一個謂詞來調用時,`filter()` 返回與其匹配的集合元素。<!-- -->對于 `List` 和` Set`,過濾結果都是一個 `List`,對 `Map` 來說結果還是一個 `Map`。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 } println(longerThan3) val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11) val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10} println(filteredMap) //sampleEnd } ``` `filter()` 中的謂詞只能檢查元素的值。<!-- -->如果想在過濾中使用元素在集合中的位置,應該使用 [`filterIndexed()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-indexed.html)。<!-- -->它接受一個帶有兩個參數的謂詞:元素的索引和元素的值。 如果想使用否定條件來過濾集合,請使用 [`filterNot()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-not.html)。<!-- -->它返回一個讓謂詞產生 `false` 的元素列表。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val filteredIdx = numbers.filterIndexed { index, s -> (index != 0) && (s.length < 5) } val filteredNot = numbers.filterNot { it.length <= 3 } println(filteredIdx) println(filteredNot) //sampleEnd } ``` 還有一些函數能夠通過過濾給定類型的元素來縮小元素的類型: * [`filterIsInstance()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-is-instance.html) 返回給定類型的集合元素。在一個 `List<Any>` 上被調用時,`filterIsInstance<T>()` 返回一個 `List<T>`,從而讓你能夠在集合元素上調用 `T` 類型的函數。 ```kotlin fun main() { //sampleStart val numbers = listOf(null, 1, "two", 3.0, "four") println("All String elements in upper case:") numbers.filterIsInstance<String>().forEach { println(it.toUpperCase()) } //sampleEnd } ``` * [`filterNotNull()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/filter-not-null.html) 返回所有的非空元素。在一個 `List<T?>` 上被調用時,`filterNotNull()` 返回一個 `List<T: Any>`,從而讓你能夠將所有元素視為非空對象。 ```kotlin fun main() { //sampleStart val numbers = listOf(null, "one", "two", null) numbers.filterNotNull().forEach { println(it.length) // 對可空的 String 來說長度不可用 } //sampleEnd } ``` ## 劃分 另一個過濾函數 – [`partition()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/partition.html) – 通過一個謂詞過濾集合并且將不匹配的元素存放在一個單獨的列表中。<!-- -->因此,你得到一個 `List` 的 `Pair` 作為返回值:第一個列表包含與謂詞匹配的元素并且第二個列表包含原始集合中的所有其他元素。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val (match, rest) = numbers.partition { it.length > 3 } println(match) println(rest) //sampleEnd } ``` ## 檢驗謂詞 最后,有些函數只是針對集合元素簡單地檢測一個謂詞: * 如果至少有一個元素匹配給定謂詞,那么 [`any()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/any.html) 返回 `true`。 * 如果沒有元素與給定謂詞匹配,那么 [`none()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/none.html) 返回 `true`。 * 如果所有元素都匹配給定謂詞,那么 [`all()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/all.html) 返回 `true`。注意,在一個空集合上使用任何有效的謂詞去調用 `all()` 都會返回 `true` 。這種行為在邏輯上被稱為 [_vacuous truth_](https://en.wikipedia.org/wiki/Vacuous_truth)。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") println(numbers.any { it.endsWith("e") }) println(numbers.none { it.endsWith("a") }) println(numbers.all { it.endsWith("e") }) println(emptyList<Int>().all { it > 5 }) // vacuous truth //sampleEnd } ``` `any()` 和 `none()` 也可以不帶謂詞使用:在這種情況下它們只是用來檢查集合是否為空。如果集合中有元素,`any()` 返回 `true`,否則返回 `false`;`none()` 則相反。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val empty = emptyList<String>() println(numbers.any()) println(empty.any()) println(numbers.none()) println(empty.none()) //sampleEnd } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看