2017年8月22日火曜日

Kotlin メモ : mapNotNull

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-not-null.html


val list = (0 until adapter.count) .map { adapter.item(it) } .filterNotNull() .toList() mapNotNull 使用 val list = (0 until adapter.count) .mapNotNull { adapter.item(it) } .toList()

Kotlin メモ : until

https://kotlinlang.org/docs/reference/ranges.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.ranges/until.html


val list = (0..adapter.count - 1) .map { adapter.item(it) } .filterNotNull() .toList() until 使用 val list = (0 until adapter.count) .map { adapter.item(it) } .filterNotNull() .toList()

Kotlin メモ : filterNotNull

filterNotNull


Java final List<Item> list = new ArrayList<>(); for (int i = 0, count = adapter.getCount(); i < count; i++) { final Item item = adapter.getItem(i); if (item != null) { list.add(item); } } Kotlin 自動変換直後 val list = ArrayList<Item>() var i = 0 val count = adapter.count while (i < count) { val item = adapter.item(i) if (item != null) { list.add(item) } i++ } range, let 使用 val list = ArrayList<Item>() for(i in 0..adapter.count - 1) { adapter.item(i)?.let { list.add(it) } } map, filterNotNull 使用 val list = (0..adapter.count - 1) .map { adapter.item(it) } .filterNotNull() .toList()