Android 为什么在kotlin中启动flatMap时需要调用toList()?

Android 为什么在kotlin中启动flatMap时需要调用toList()?,android,kotlin,Android,Kotlin,当我使用my.flatMap{it.toList()}时,我可以得到正确的结果 我认为valmy是一个List,所以我不需要调用toList(),但是代码var ok3=my.flatMap{it}无法编译,为什么 而且,代码var ok2=my.flatMap{bb}会得到一个错误结果,为什么 代码A var my=listOf("abc","cef") var bb=my.toList() var ok1=my.flatMap {it.toList()} var ok2=my.flatMa

当我使用
my.flatMap{it.toList()}
时,我可以得到正确的结果

我认为val
my
是一个
List
,所以我不需要调用
toList()
,但是代码
var ok3=my.flatMap{it}
无法编译,为什么

而且,代码
var ok2=my.flatMap{bb}
会得到一个错误结果,为什么

代码A

var my=listOf("abc","cef")
var bb=my.toList()

var ok1=my.flatMap {it.toList()}
var ok2=my.flatMap { bb }
//var ok3=my.flatMap { it }  //It's wrong
结果

至Naetmul:

谢谢

为什么您认为
it
String
类型的代码
val ok3=my.flatMap{it}

在我看来,
it
应该是lambda表达式中的
my
类型,
my
List
类型

图像
侧注:如果不重新分配,
val
var
更受欢迎


这里,
my
的类型是
List
,其元素是
“abc”
“cef”

bb
的类型是
List
,因为
List
被转换为
List

它的元素是
“abc”
“cef”

i、 例如,
my
bb
是等效的,尽管它们是不同的实例。您不需要
bb


这类似于

var ok1 = listOf<Char>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    val chars: List<Char> = it.toList() // String is transformed into List of Chars

    ok1 = ok1 + chars
}
var ok2 = listOf<String>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok2 = ok1 + bb // You know, bb = listOf("abc, "cef")
}
var ok3 = listOf<?>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok3 = ok3 + it
}
这类似于

var ok1 = listOf<Char>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    val chars: List<Char> = it.toList() // String is transformed into List of Chars

    ok1 = ok1 + chars
}
var ok2 = listOf<String>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok2 = ok1 + bb // You know, bb = listOf("abc, "cef")
}
var ok3 = listOf<?>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok3 = ok3 + it
}
这类似于

var ok1 = listOf<Char>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    val chars: List<Char> = it.toList() // String is transformed into List of Chars

    ok1 = ok1 + chars
}
var ok2 = listOf<String>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok2 = ok1 + bb // You know, bb = listOf("abc, "cef")
}
var ok3 = listOf<?>() // empty List
for (it in my) {
    // `it` is the `String` type.
    // For the 1st iteration, it = "abc"
    // For the 2nd iteration, it = "cef"

    ok3 = ok3 + it
}

请参阅

fun Iterable.flatMap(
变换:(T)->Iterable
):列表
返回从的结果生成的所有元素的单个列表 在原始文件的每个元素上调用的transform函数 收藏

假设变量
list
的类型为
list

如果我们想执行
val newList=list.flatMap(函数)
,那么

函数
应具有函数类型
(T)->Iterable

function
的参数是
T
-类型,它retuns
Iterable
List
Collection
的子类型,
Collection是
Iterable
的子类型,因此
List
Iterable`的子类型。)

newList
将具有类型
List

问题中的
将是
函数的参数

因此,
it
将是
T
-type,而不是
List
-type。
i、 例如,
it
将是
String
-类型,而不是
List
-类型。

1

var my=listOf("abc","cef")
var ok1=my.flatMap {it.toList()}
//var ok3=my.flatMap { it }  //It's wrong
在这种情况下:
-
flatMap
A
转换为
Iterable

-
it
的类型是
String

-
String.toList()
返回
List

var ok3=my.flatMap{it}
是错误的,因为您需要将
String
转换为
Iterable
like
List

2.

var ok2=my.flatMap { bb } get an error result
my
有2个元素,因此
my.flatMap{bb}
对返回执行两次
bb


返回两次
bb

编译错误是什么?谢谢!你能看看我在问题中添加的内容吗?@HelloCW更新。
flatMap
所做的是:对于列表中的每个元素,应用函数(返回列表或集合),然后连接函数的返回值(列表或集合)。