Kotlin 嵌套 for 循环到 asSequence
Kotlin nested for loops to asSequence
我正在尝试将我的嵌套 for 循环转换为 Kotlin 中的 asSequence
。在这里,我的目标是 从 另一个具有相同键 的对象数组 获取并更新我所有对象数组 的值。
嵌套 for 循环:
val myFields = getMyFields()
val otherFields = getOtherFields()
for (myField in myFields) { // loop tru the my fields
for (otherField in otherFields) { // find the same fields
if (myField.key == otherField.key) { // if the same, update the value
val updatedMyField = myField.copy(value = otherValue.value)
myFields[myFields.indexOf(myField)] = updatedMyField // update my field value
break
}
}
}
我尝试过的:
val updatedMyFields = getMyFields().asSequence()
.map { myField ->
getOtherFields().asSequence()
.map { otherField ->
if (myField.key == otherField.key) {
return@map otherField.value
} else {
return@map ""
}
}
.filter { it?.isNotEmpty() == true }
.first()?.map { myField.copy(value = it.toString()) }
}
.toList()
但这不会编译 return List<List<MyField>>
.
我正在为此寻找更清洁的东西。
为什么要使用 asSequence() ?你可以做这样的事情:
val myFields = getMyFields()
val otherFields = getOtherFields()
myFields.forEach{firstField ->
otherFields.forEach{secondField ->
if (firstField.key == secondField.key) {
myFields[myFields.indexOf(firstField)] = secondField.value
}
}
}
这将完成与嵌套 for 循环相同的工作,并且比嵌套 asSequence() 更易于阅读、理解和维护。
正如评论所暗示的那样,使用 Map
可能会更有效率。
(更准确地说,映射解决方案所花费的时间与列表长度的 总和 成正比,而嵌套 for 循环所花费的时间与其 乘积成正比 — 变大得更快。)
这是一种方法:
val otherFields = getOtherFields().associate{ it.key to it.value }
val myFields = getMyFields().map {
val otherValue = otherFields[it.key]
if (otherValue != null) it.copy(value = otherValue) else it
}
第一行创建了一个 Map
从“其他字段”键到它们的值。然后其余的使用它从“我的字段”创建一个新列表,替换存在的“其他字段”中的值。
我不得不对类型 &c 做出假设,因为问题中的代码不完整,但这应该做同样的事情。显然,您可以通过修改 it.copy()
.
来更改它合并值的方式
根据周围的代码,可能还有更简单、更有效的方法。如果您将其扩展为 Minimal, Complete, and Verifiable Example——特别是,根据您的评论,说明您已经如何使用 Map
——我们可能会提出更好的建议。
我正在尝试将我的嵌套 for 循环转换为 Kotlin 中的 asSequence
。在这里,我的目标是 从 另一个具有相同键 的对象数组 获取并更新我所有对象数组 的值。
嵌套 for 循环:
val myFields = getMyFields()
val otherFields = getOtherFields()
for (myField in myFields) { // loop tru the my fields
for (otherField in otherFields) { // find the same fields
if (myField.key == otherField.key) { // if the same, update the value
val updatedMyField = myField.copy(value = otherValue.value)
myFields[myFields.indexOf(myField)] = updatedMyField // update my field value
break
}
}
}
我尝试过的:
val updatedMyFields = getMyFields().asSequence()
.map { myField ->
getOtherFields().asSequence()
.map { otherField ->
if (myField.key == otherField.key) {
return@map otherField.value
} else {
return@map ""
}
}
.filter { it?.isNotEmpty() == true }
.first()?.map { myField.copy(value = it.toString()) }
}
.toList()
但这不会编译 return List<List<MyField>>
.
我正在为此寻找更清洁的东西。
为什么要使用 asSequence() ?你可以做这样的事情:
val myFields = getMyFields()
val otherFields = getOtherFields()
myFields.forEach{firstField ->
otherFields.forEach{secondField ->
if (firstField.key == secondField.key) {
myFields[myFields.indexOf(firstField)] = secondField.value
}
}
}
这将完成与嵌套 for 循环相同的工作,并且比嵌套 asSequence() 更易于阅读、理解和维护。
正如评论所暗示的那样,使用 Map
可能会更有效率。
(更准确地说,映射解决方案所花费的时间与列表长度的 总和 成正比,而嵌套 for 循环所花费的时间与其 乘积成正比 — 变大得更快。)
这是一种方法:
val otherFields = getOtherFields().associate{ it.key to it.value }
val myFields = getMyFields().map {
val otherValue = otherFields[it.key]
if (otherValue != null) it.copy(value = otherValue) else it
}
第一行创建了一个 Map
从“其他字段”键到它们的值。然后其余的使用它从“我的字段”创建一个新列表,替换存在的“其他字段”中的值。
我不得不对类型 &c 做出假设,因为问题中的代码不完整,但这应该做同样的事情。显然,您可以通过修改 it.copy()
.
根据周围的代码,可能还有更简单、更有效的方法。如果您将其扩展为 Minimal, Complete, and Verifiable Example——特别是,根据您的评论,说明您已经如何使用 Map
——我们可能会提出更好的建议。