Bernhard
09/02/2019, 2:04 PMval list: List<Pair<String, String>> = listOf(
"a" to null,
"b" to "hi"
)
.filter {it.second != null}Alowaniak
09/02/2019, 2:06 PMlistOf("a" to "b", "c" to null).mapNotNull { if(it.second == null) null else it }
Maybe you would need a it as Pair<String, String> but idkLuca Nicoletti
09/02/2019, 2:08 PMif (it.second == null) null else it is useless, just use .mapNotNull { it.second }Bernhard
09/02/2019, 2:08 PMLuca Nicoletti
09/02/2019, 2:09 PMAlowaniak
09/02/2019, 2:09 PM.mapNotNull { it.second } would map to the second element, he would indeed lose the it.firstBernhard
09/02/2019, 2:09 PMBernhard
09/02/2019, 2:09 PMAlowaniak
09/02/2019, 2:10 PMas Pair<String, String> ?Bernhard
09/02/2019, 2:10 PMBernhard
09/02/2019, 2:10 PMBernhard
09/02/2019, 2:11 PMBernhard
09/02/2019, 2:12 PMAlowaniak
09/02/2019, 2:16 PMfun nonNullPair(from: Pair<String, String?>): Pair<String, String>? {
return from.first to (from.second ?: return null)
}
fun main() {
val xs: List<Pair<String, String>> = listOf("a" to "b", "c" to null).mapNotNull(::nonNullPair)
println(xs)
}karelpeeters
09/02/2019, 5:10 PMAlowaniak
09/03/2019, 6:34 AM