Collect conditional items while steaming in kotlin
In Kotlin code, I have a list of objects and while processing it via filter and map I want to collect items of particular interest. And discard others.
For example, I am using foreach loop as below. Is it possible to make it better by using map instead of foreach?
fun main() {
val exceptionRequests = mutableListOf()
listOf("Name1", "Name2", "Name3")
.filter {
it.length > 2
}
.forEach {
try {
if (it == "Name2") {
throw Exception(it)...