Home > database >  Cannot find all match value in nested list Kotlin
Cannot find all match value in nested list Kotlin

Time:10-20

Hey I have list i want to find all matching item. I tried some code but it not working all finding items. I need to do in efficient way. I guess my below code returns first matching item.

Group

data class Group(
    val key: Int,
    val value: MutableList<GroupValue?>
)

GroupValue

data class GroupValue(
    val isRead: Boolean? = null,
    val id: String? = null
)

Main.kt

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)
        }
    }
}

Output getting through above code

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)

Expected output

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)

CodePudding user response:

You need to use filter() instead of find() (find() gets you only a single object but you want multiple) as follows:

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)
        }
    }
}

This will give you the following output:

[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)]

In order to get what you want you need to change the code a bit more to map each Group object to a list of GroupValue and then flatten the list of lists. Check the following code:

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) }
}
  • Related