Hey i have nested list and i wanted to find last occurrence index value of inner list and outer list. I tried to search Find last occurrence of a String in array using Kotlin. How can I do this in my case in efficient way.
Group
data class Group(
val key: Int,
val value: MutableList<GroupValue?>
)
GroupValue
data class GroupValue(
val isRead: Boolean? = null,
val id: String? = null
)
Condition
isRead != true
For example
Scenario 1
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"))),
)
Excepted output
inner list index is 0 and outer list index is 4
Scenario 2
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, "6"),GroupValue(false, "7"))),
Group(7, mutableListOf(GroupValue(true, "7")))
)
Excepted output
inner list index is 1 and outer list index is 6
Inside this Find first occurrence index value in efficient way in kotlin I asked for first index but now I need in last occurrence .
Thanks in advance.
CodePudding user response:
You can use indexOfLast
and indexOfFirst
functions for this
var innerIndex = -1
val outerIndex: Int = value.indexOfLast{ group ->
innerIndex = group.value.indexOfFirst { groupValue -> groupValue != null && groupValue.isRead != true }
return@indexOfLast innerIndex != -1
}
if(outerIndex != -1) {
// match found
}