Last active
July 30, 2019 07:39
-
-
Save ch8n/7563b02d3aee0f473c6436322cf1e388 to your computer and use it in GitHub Desktop.
Possible destructing options in kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Data Class Destructuring | |
| val person = Person(1, "Chetan", 25) | |
| val(id, name, age) = person | |
| //Collections Destructuring | |
| //map | |
| val map: HashMap<Int, Person> = HashMap() | |
| map.put(1, person) | |
| map.forEach { entry-> | |
| val(key,value) = entry | |
| } | |
| //List or Arrays | |
| val threeItemList = listOf("one", "two", "three") | |
| val (itemOne, itemtwo) = threeItemList | |
| //Destructuring in Lambda | |
| map.forEach { (key,value)-> ...} | |
| //Destructuring in for loop | |
| for ((key, value) in map) { ... } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment