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()}
时,我可以得到正确的结果
我认为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.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
-类型,它retunsIterable
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
likeList
2.
var ok2=my.flatMap { bb } get an error result
my
有2个元素,因此my.flatMap{bb}
对返回执行两次bb
返回两次bb
编译错误是什么?谢谢!你能看看我在问题中添加的内容吗?@HelloCW更新。flatMap
所做的是:对于列表中的每个元素,应用函数(返回列表或集合),然后连接函数的返回值(列表或集合)。