Getting the elements from a list of lists with the Java Stream API in Kotlin

Viewed 7014

The following code (written in Kotlin) extracts the elements from a list of lists. It works, but looks rather ugly and difficult to read.

Is there a nicer way to write the same with the java stream api? (Examples can be given in Kotlin or Java)

val listOfLists: List<Any> = ...
val outList: MutableList<Any> = mutableListOf()

listOfLists.forEach {
    list ->
    if (list is ArrayList<*>) list.forEach {
        l ->
        outList.add(l)
    }
}

return outList;
3 Answers
Related