![]() Little did we realize, actually this approach is also used to destructure the key-value of map entries. Well, it seems pretty limited, and we’re not likely to use it to destructure a list or array. The bracket approach although concise, it doesn’t handle nullable collection. It also compile error val listFirstItem = list? // list: List? = listOf(1) val arrayFirstItem = array? // array: Array? = arrayOf(1) val mapWithKey0 = map? // map: Map? = mapOf(0 to 1) 2. Every element on the list is first mapped and then filtered, on the contrary if we dont use sequences, it will first map all the list, and then filter the. val listFirstItem = list // list: List? = listOf(1) val arrayFirstItem = array // array: Array? = arrayOf(1) val mapWithKey0 = map // map: Map? = mapOf(0 to 1) // Neither can we do below. However, it cannot be used for Sequence // Compile Error! val sequenceFirstElement = sequence // sequence = sequenceOf(1)īesides, it can’t be used for nullable list, array, or map // Compile error, as all of them are nullable. val listFirstItem = list // list = listOf(1) val arrayFirstItem = array // array = arrayOf(1) val mapWithKey0 = map // map = mapOf(0 to 1) It can be used for the list, array, and map. ![]() ![]() This is the default recommended approach. There are 4 approach?! What’s their differences? 1. How do you do it? Which one do you use below? If you’ve had to use Kotlin Collection, you probably have access to its element before.
0 Comments
Leave a Reply. |