Ruckus
06/12/2017, 3:03 PMenum class People(value: Int) {
man(1),
baby(2),
woman(3);
companion object {
fun byValue(value: Int) = values().firstOrNull { it.value == value }
}
}
val people = People.byValue(3)
if (people != null) {
print("converted successfully => $people")
} else {
print("null value -> No Match")
}
Ruckus
06/12/2017, 3:12 PMvalues()
call can be expensive), you can use a map
enum class People(value: Int) {
man(1),
baby(2),
woman(3);
companion object {
private val map = values().associateBy(People::value)
fun byValue(value: Int) = map[value]
}
}
nawar
06/12/2017, 3:13 PM