flatMapIndexed
所在位置是kotlin.collections.flatMapIndexed
,其相關用法介紹如下。
用法一
@JvmName("flatMapIndexedIterable") inline fun <T, R> Array<out T>.flatMapIndexed(
transform: (index: Int, T) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> ByteArray.flatMapIndexed(
transform: (index: Int, Byte) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> ShortArray.flatMapIndexed(
transform: (index: Int, Short) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> IntArray.flatMapIndexed(
transform: (index: Int, Int) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> LongArray.flatMapIndexed(
transform: (index: Int, Long) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> FloatArray.flatMapIndexed(
transform: (index: Int, Float) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> DoubleArray.flatMapIndexed(
transform: (index: Int, Double) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> BooleanArray.flatMapIndexed(
transform: (index: Int, Boolean) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedIterable") inline fun <R> CharArray.flatMapIndexed(
transform: (index: Int, Char) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedSequence") inline fun <T, R> Array<out T>.flatMapIndexed(
transform: (index: Int, T) -> Sequence<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UIntArray.flatMapIndexed(
transform: (index: Int, UInt) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> ULongArray.flatMapIndexed(
transform: (index: Int, ULong) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UByteArray.flatMapIndexed(
transform: (index: Int, UByte) -> Iterable<R>
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UShortArray.flatMapIndexed(
transform: (index: Int, UShort) -> Iterable<R>
): List<R>
返回對每個元素及其在原始數組中的索引調用的 transform 函數的結果產生的所有元素的單個列表。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val data: List<String> = listOf("Abcd", "efgh", "Klmn")
val selected: List<Boolean> = data.map { it.any { c -> c.isUpperCase() } }
val result = data.flatMapIndexed { index, s -> if (selected[index]) s.toList() else emptyList() }
println(result) // [A, b, c, d, K, l, m, n]
//sampleEnd
}
輸出:
[A, b, c, d, K, l, m, n]
用法二
@JvmName("flatMapIndexedIterable") inline fun <T, R> Iterable<T>.flatMapIndexed(
transform: (index: Int, T) -> Iterable<R>
): List<R>
@JvmName("flatMapIndexedSequence") inline fun <T, R> Iterable<T>.flatMapIndexed(
transform: (index: Int, T) -> Sequence<R>
): List<R>
返回對每個元素及其在原始集合中的索引調用的 transform 函數的結果產生的所有元素的單個列表。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val data: List<String> = listOf("Abcd", "efgh", "Klmn")
val selected: List<Boolean> = data.map { it.any { c -> c.isUpperCase() } }
val result = data.flatMapIndexed { index, s -> if (selected[index]) s.toList() else emptyList() }
println(result) // [A, b, c, d, K, l, m, n]
//sampleEnd
}
輸出:
[A, b, c, d, K, l, m, n]
相關用法
- Kotlin flatMapIndexed用法及代碼示例
- Kotlin flatMap用法及代碼示例
- Kotlin flatten用法及代碼示例
- Kotlin filterNot用法及代碼示例
- Kotlin filterNotTo用法及代碼示例
- Kotlin filterKeys用法及代碼示例
- Kotlin filterTo用法及代碼示例
- Kotlin filterNotNull用法及代碼示例
- Kotlin forEach用法及代碼示例
- Kotlin filterIsInstance用法及代碼示例
- Kotlin filterIsInstanceTo用法及代碼示例
- Kotlin findLast用法及代碼示例
- Kotlin find用法及代碼示例
- Kotlin fold用法及代碼示例
- Kotlin firstNotNullOf用法及代碼示例
- Kotlin foldTo用法及代碼示例
- Kotlin firstNotNullOfOrNull用法及代碼示例
- Kotlin filterNotNullTo用法及代碼示例
- Kotlin filterIndexedTo用法及代碼示例
- Kotlin filterValues用法及代碼示例
- Kotlin filter用法及代碼示例
- Kotlin filterIndexed用法及代碼示例
- Kotlin associateBy用法及代碼示例
- Kotlin all用法及代碼示例
- Kotlin map用法及代碼示例
注:本文由純淨天空篩選整理自kotlinlang.org大神的英文原創作品 kotlin.collections.flatMapIndexed。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。