-
Notifications
You must be signed in to change notification settings - Fork 22
Collections: Break the list of items to sub‐chunks of items
Devrath edited this page Feb 29, 2024
·
1 revision
Code
data class Country(val name: String, val isDemocracy: Boolean)
val countriesList = mutableListOf(
Country(name = "USA", isDemocracy = true),
Country(name = "Australia", isDemocracy = true),
Country(name = "Russia", isDemocracy = false),
Country(name = "England", isDemocracy = true),
Country(name = "North Korea", isDemocracy = false)
)
fun main(args: Array<String>) {
val output = countriesList.chunked(2)
println("Result-> $output")
}
Output
Result-> [[Country(name=USA, isDemocracy=true), Country(name=Australia, isDemocracy=true)], [Country(name=Russia, isDemocracy=false), Country(name=England, isDemocracy=true)], [Country(name=North Korea, isDemocracy=false)]]