Accessing an attribute of a map Entry (Kotlin)

时间:2015-10-29 15:42:57

标签: kotlin

I have the following class:

class Entry1(var type:String, var kind:String, var index:Int)

And a map:

var map1 = mutableMapOf<String, Entry>()

How would I access the attributes of Entry1 of a given map entry?

Example: Say I have:

map1["ex1"] = Entry("ex2","ex3",4)

Now I want to get the indexfrom the Entry I've added.

How can that be done?

2 个答案:

答案 0 :(得分:0)

val entry = map1["ex1"]
if (entry != null) {
    val index = entry.index;
    ...
}

答案 1 :(得分:0)

val entry = map1["ex1"]
val index = entry?.let{
  entry.index
} ?: -1 //optional