嘿,我有串列,我想找到所有匹配的專案。我嘗試了一些代碼,但它不能用于所有查找專案。我需要以有效的方式做。我猜我下面的代碼回傳第一個匹配項。
團體
data class Group(
val key: Int,
val value: MutableList<GroupValue?>
)
組值
data class GroupValue(
val isRead: Boolean? = null,
val id: String? = null
)
主檔案
fun main() {
val value = listOf(
Group(0, mutableListOf(GroupValue(true, "1"))),
Group(1, mutableListOf(GroupValue(true, "2"))),
Group(2, mutableListOf(GroupValue(false, "3"))),
Group(3, mutableListOf(GroupValue(true, "4"))),
Group(4, mutableListOf(GroupValue(false, "5"))),
Group(5, mutableListOf(GroupValue(true, "6"))),
Group(6, mutableListOf(GroupValue(true, "7"), GroupValue(false, "8"))),
Group(7, mutableListOf(GroupValue(true, "9"), GroupValue(false, "10"))),
Group(8, mutableListOf(GroupValue(false, "11"), GroupValue(false, "12"), GroupValue(false, "13"))),
Group(9, mutableListOf(GroupValue(false, "14"), GroupValue(true, "15"))),
Group(10, mutableListOf(GroupValue(true, "16")))
)
val list = value.slice(2..9)
var groupValue: GroupValue? = null
list.forEach { messageGroup ->
groupValue = messageGroup.value.find { it?.isRead == false }
if (groupValue != null) {
println(groupValue)
}
}
}
通過上面的代碼輸出
GroupValue(isRead=false, id=3)
GroupValue(isRead=false, id=5)
GroupValue(isRead=false, id=8)
GroupValue(isRead=false, id=10)
GroupValue(isRead=false, id=11)
GroupValue(isRead=false, id=14)
預期輸出
GroupValue(isRead=false, id=3)
GroupValue(isRead=false, id=5)
GroupValue(isRead=false, id=8)
GroupValue(isRead=false, id=10)
GroupValue(isRead=false, id=11)
GroupValue(isRead=false, id=12)
GroupValue(isRead=false, id=13)
GroupValue(isRead=false, id=14)
uj5u.com熱心網友回復:
您需要使用filter()代替find()(find()讓您只獲得一個物件,但您需要多個),如下所示:
fun main() {
val value = listOf(
Group(0, mutableListOf(GroupValue(true, "1"))),
Group(1, mutableListOf(GroupValue(true, "2"))),
Group(2, mutableListOf(GroupValue(false, "3"))),
Group(3, mutableListOf(GroupValue(true, "4"))),
Group(4, mutableListOf(GroupValue(false, "5"))),
Group(5, mutableListOf(GroupValue(true, "6"))),
Group(6, mutableListOf(GroupValue(true, "7"), GroupValue(false, "8"))),
Group(7, mutableListOf(GroupValue(true, "9"), GroupValue(false, "10"))),
Group(8, mutableListOf(GroupValue(false, "11"), GroupValue(false, "12"), GroupValue(false, "13"))),
Group(9, mutableListOf(GroupValue(false, "14"), GroupValue(true, "15"))),
Group(10, mutableListOf(GroupValue(true, "16")))
)
val list = value.slice(2..9)
list.forEach { messageGroup ->
val groupValues = messageGroup.value.filter { it?.isRead == false }
if (groupValues.isNotEmpty()) {
println(groupValues)
}
}
}
這將為您提供以下輸出:
[GroupValue(isRead=false, id=3)]
[GroupValue(isRead=false, id=5)]
[GroupValue(isRead=false, id=8)]
[GroupValue(isRead=false, id=10)]
[GroupValue(isRead=false, id=11), GroupValue(isRead=false, id=12), GroupValue(isRead=false, id=13)]
[GroupValue(isRead=false, id=14)]
為了獲得您想要的東西,您需要對代碼進行更多更改以將每個Group物件映射到一個串列,GroupValue然后將串列串列展平。檢查以下代碼:
fun main() {
val value = listOf(
Group(0, mutableListOf(GroupValue(true, "1"))),
Group(1, mutableListOf(GroupValue(true, "2"))),
Group(2, mutableListOf(GroupValue(false, "3"))),
Group(3, mutableListOf(GroupValue(true, "4"))),
Group(4, mutableListOf(GroupValue(false, "5"))),
Group(5, mutableListOf(GroupValue(true, "6"))),
Group(6, mutableListOf(GroupValue(true, "7"), GroupValue(false, "8"))),
Group(7, mutableListOf(GroupValue(true, "9"), GroupValue(false, "10"))),
Group(8, mutableListOf(GroupValue(false, "11"), GroupValue(false, "12"), GroupValue(false, "13"))),
Group(9, mutableListOf(GroupValue(false, "14"), GroupValue(true, "15"))),
Group(10, mutableListOf(GroupValue(true, "16")))
)
val list = value.slice(2..9)
list.map { messageGroup -> messageGroup.value.filter { it?.isRead == false } }
.flatten()
.forEach { println(it) }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/325933.html
標籤:科特林
