Kotlin replace isEmpty()&last() with lastOrNull() within a Collection

Viewed 574

I would like to use something like (code below), but I think that there must be a nicer solution with usage lastOrNull() instead of using isEmpty and last()

data class Entry(val x: Float, val y: Float)

var entries: MutableList<Entry> = ArrayList()
if(some) {
  entries.add(Entry(100f, 200f)
}
val foo = (if (entries.isEmpty()) 0f else entries.last().y) + 100f

Is there some better way like entries.lastOrNull()?.y if null 0f?

2 Answers
Related