I want to make something like break
in the nested forEach loop to filter data in my searchView (if data content contains any word in my search).
val filtered = mutableListOf<EventEntity>()
rawDataList.forEach {data ->
text.split(' ').forEach { word ->
if (data.content.contains(word, ignoreCase = true)) {
filtered.add(data)
return@forEach // **There is more than one label with such a name in this scope**
}
}
}
Does elegant solution exist in my case?
If you ever encounter this error and won't be able to fix it with built in function you can apply custom labels to lambdas by adding name@
before the block:
rawDataList.forEach outer@{data ->
text.split(' ').forEach { word ->
if (data.content.contains(word, ignoreCase = true)) {
filtered.add(data)
return@outer
}
}
}
Seems like the any
extension method is what you're looking for. From the javadoc:
Returns true
if at least one element matches the given [predicate].
val filtered = rawDataList.filter { data ->
text.split(' ').any { data.content.contains(it, ignoreCase = true) }
}.toMutableList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With