Issue
It can be done easily in Java.
for(Event event:eventList){
if(event.id.equalTo(eventId){
Event e=new Event();
e=event;
}
}
I'm doing like this in Kotlin but expected result is not coming
fun filterList(listCutom: List<Event>?) {
listCutom!!.forEachIndexed { index, eventid ->
if (listCutom[index].eventTypeId.equals(eventType)) {
event= eventid
}
}
}
how can I do with Kotlin using filter
or forEachIndexed
or in another way efficiently?
Solution
Why do you use forEachIndexed
in Kotlin code, since you don't need the index?
I don't know if the loop could find more than 1 objects and how you use e
:
listCutom!!.forEach { event ->
if (event.id.equalTo(eventId)) {
val e = event
//.................
}
}
with filter:
val filtered = listCutom!!.filter { it.id.equalTo(eventId) }
filtered.forEach { ... }
If you want the indices of the list items that conform to a certain condition:
val indices = listCutom!!.mapIndexedNotNull { index, event -> if (event.id.equalTo(eventId)) index else null}
then you can iterate through the indices
list:
indices.forEach { println(it) }
Answered By - forpas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.