https://kotlinlang.org logo
Title
s

Syed Faraz

07/19/2019, 9:10 AM
how do i use
mapto
plus
takeif
in chain, so that only those which satisfy operation are mapped to an arraylist?
w

wasyl

07/19/2019, 9:20 AM
You want to take only certain items from the collection, map them to other object, and put everything in an array list? In that case I think it’d be
collection.filter { }.mapTo(arrayList()) { }
t

tseisel

07/19/2019, 9:28 AM
collection.mapNotNullTo(arrayListOf()) { item ->
    item.transform().takeIf { it.satisfiesPredicate() }
}
g

gildor

07/19/2019, 9:29 AM
Yes, Thibault was faster 🙂 mapNotNullTo is the way to do that, but only for non-nullable values