Matheus
01/08/2019, 3:12 PMval item1: String = ""
val item2: String? = getItem2()
val map = mapOf(1 to item1, 2 to item2) //only if item2 is not null
Dominaezzz
01/08/2019, 3:19 PMval map = if (item2 != null) mapOf(1 to item1, 2 to item2) else mapOf(1 to item1)
.Dominaezzz
01/08/2019, 3:22 PMMatheus
01/08/2019, 3:27 PMmapOf(1 to item1, 2 to item2)
.filter {it.value != null}
.map{ Pair(it.key, it.value!!)}
Dominaezzz
01/08/2019, 3:31 PMmap.filterValues { it != null }
.mapValues { it.value!! }
.Artem Golovko
01/08/2019, 3:31 PMfun main() {
val allItems = listOf(
"",
getItem2()
)
val map = allItems.filterNotNull().mapIndexed { index, el -> index to el }
}
fun getItem2(): String? {
val rnd = Random.nextInt()
if (rnd % 2 == 0) {
return "Not null string"
}
return null
}
Matheus
01/08/2019, 4:02 PM