Exercise: Crime analysis
Your task is to analyze crime data from Chicago in order to count the number of crimes per primary cause. You’ve implemented the solution, but it fails due to OutOfMemoryError
. Fix it.
fun main() {
measureTimeMillis {
File("Crimes_-_2001_to_Present.csv")
.readLines()
.drop(1)
.map { Crime.parse(it) }
.groupBy { it.primaryType }
.mapValues { it.value.size }
.toList()
.sortedByDescending { (_, num) -> num }
.joinToString(separator = "\n") { (type, num) ->
"$num $type"
}
.let(::println)
}.let { println("Took $it") }
}
Download data in CSV format from the following link:
Compare execution time of the following solutions that use the groupBy
function to group elements, and the same solutions that use groupingBy
function to group elements.
Once you are done with the exercise, you can check your solution here.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.