Skip to content

Instantly share code, notes, and snippets.

@pravinlondhe
Created June 1, 2020 05:43
Show Gist options
  • Save pravinlondhe/10b0bfeba1bb587ca05bbbe8f2611074 to your computer and use it in GitHub Desktop.
Save pravinlondhe/10b0bfeba1bb587ca05bbbe8f2611074 to your computer and use it in GitHub Desktop.
Kotlin collections
fun main() {
//input data
val kotlin = Language("kotlin")
val java = Language("java")
java.town = "San Francisco"
val php = Language("php")
val scala = Language("scala")
scala.town = "New York"
val javascript = Language("javascript")
val languagesList = listOf(kotlin, java, php, scala, javascript)
// count
println(languagesList.count()) // 5
//maxBy
println(languagesList.maxBy { it.name.length }) // javascript object
//minBy
println(languagesList.minBy { it.name.length }) // php object
//average
println(arrayOf(8, 8, 10, 9, 9).average()) //8.8
//sum
println(arrayOf(7.3, 7.6, 7.4, 7.6, 8.1, 7.7).sum()) //45.7
println(languagesList.sumBy { it.releaseYear }) // 10050
//first
//NoSuchElementException could be thrown
println(languagesList.first()) // kotlin object
println(languagesList.first { it.name.length == 3 }) // php object
println(languagesList.firstOrNull { it.name.length == 2 }) // null
//find
println(languagesList.find { it.town == "San Francisco" }) //java object
println(languagesList.findLast { it.town == "Bucharest" }) //null
//last
println(languagesList.last()) // javascript object
println(languagesList.last { it.town.contains(' ') }) //scala object
//single
//IllegalArgumentException if list has more than one element
println(languagesList.single{ it.town == "San Francisco" }) // java object
println(languagesList.singleOrNull{ it.town == "Pitesti" }) // null
//filter
println(languagesList.filter { it.name.contains('k') }) // list containing kotlin object
//filterNot(), filterNotNull()
println(languagesList.filterNot { it.name.contains('k') }) // list of languages other kotlin
println(languagesList.filterNotNull()) // whole list of languages
//filterTo(), filterNotTo()
val listToFilterInto = mutableListOf<Language>()
languagesList.filterTo(listToFilterInto) { it.town.contains('o') } // list of scala object
languagesList.filterNotTo(listToFilterInto) { it.town.contains('o') } // list of languages other than scala
languagesList.filterNotNullTo(listToFilterInto) // list of languages
//filterIndexed()
println(languagesList.filterIndexed { index, _ -> index % 3 == 0}) // list of scala
//map
println(languagesList.map { it.town }) // list of town
println(languagesList.mapIndexed { index, language -> index to language.town }) // map of index to town
//distinct
println(languagesList.map { it.town }.distinct())
//associate(), associateBy()
println(languagesList.associate { it.name to it.town })
// println(languagesList.associateBy({ it.name }, { it.company }))
//groupBy()
// println(languagesList.groupBy { it.company })
}
data class Language(val name: String, var town: String = "default", val releaseYear: Int = 2010)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment