Share
Embed
Transcript
51 slides
-
1. Helps making the code much more concise 2. Part
of kotlin-stdlib 3. Built on top of Java Collections API Kotlin.Collections highlights -
data class Movie( val title: String, val releaseDate: Date, val
genre: String, val averageRating: Double) Model -
val madMax = Movie("Mad Max", "2015-05-13".toDate(), "action", 7.3) val dunkirk
= Movie("Dunkirk", "2017-07-19".toDate(), "action", 7.6) val birdman = Movie("Birdman", "2014-10-17".toDate(), "drama", 7.4) val titanic = Movie("Titanik", "1997-12-19".toDate(), "romance", 7.6) val lamb = Movie("The Silence of the Lambs", "1991-01-30".toDate(), "thriller", 8.1) val nemo = Movie("Finding Nemo", "2003-05-30".toDate(), "animation", 7.7) Some movies -
List and Mutable List val movieList = listOf(madMax, dunkirk, birdman,
titanic, lamb, nemo) val movieMutableList = mutableListOf(madMax, dunkirk, birdman, titanic, lamb, nemo) -
Set and Mutable Set val movieSet = setOf(madMax, dunkirk, birdman,
titanic, lamb, nemo) val movieMutableSet = mutableSetOf(madMax, dunkirk, birdman, titanic, lamb, nemo) Map and Mutable Map val movieMap = mapOf(madMax.title to madMax, dunkirk.title to dunkirk) val movieMutableMap = mutableMapOf(madMax.title to madMax, dunkirk.title to dunkirk) // PROTIP madMax.title to madMax == Pair(madMax.title, madMax) -
MaxBy() movieList.maxBy { it.averageRating } // Max rating: The Silence
of the Lambs, 8.1 Note: max() if element is a number -
MinBy() movieList.minBy { it.averageRating } // Min rating: Mad Max,
7.3 Note: min() if element is a number -
Sum() arrayOf(7.3, 7.6, 7.4, 7.6, 8.1, 7.7).sum() // Sum: 45.7
movieList.sumByDouble { it.averageRating } -
First() movieList.first { it.averageRating < 7.5 } // First: Mad
Max movieList.first { it.averageRating < 3.0 } // NoSuchElementException -
Find() movieList.find { it.averageRating < 7.5 } // First: Mad
Max movieList.find { it.averageRating < 3.0 } // First: null // movieList.find() == movieList.firstOrNull() -
Last() movieList.last { it.averageRating < 7.5 } // First: Birdman
movieList.last { it.averageRating < 3.0 } // NoSuchElementException -
FindLast() movieList.findLast { it.averageRating < 7.5 } // First: Birdman
movieList.findLast { it.averageRating < 3.0 } // First: null // movieList.findLast() == movieList.lastOrNull() -
Single() movieList.single { it.averageRating == 7.7 } // Single: Finding
Nemo movieList.single { it.averageRating == 7.6 } // IllegalArgumentException -
SingleOrNull() movieList.singleOrNull { it.averageRating == 7.7 } // Single: Finding
Nemo movieList.singleOrNull { it.averageRating == 7.6 } // Single: null -
Filter() movieList.filter { it.averageRating > 7.5 } // Better than
7.5: // [Dunkirk, Titanik, The Silence of the Lambs, Finding Nemo] -
FilterNot(), FilterNotNull() movieList.filterNot { it.averageRating > 7.5 } // Not
better than 7.5: [Mad Max, Birdman] movieList.filterNotNull() // Kinda useless here -
FilterTo(), filterNotTo() val listToFilterInto = mutableListOf<Movie>() movieList.filterTo(listToFilterInto, { it.averageRating >
7.5 }) // Better than 7.5: [Dunkirk, Titanik, The Silence of the Lambs, Finding Nemo] movieList.filterNotTo(listToFilterInto, { it.averageRating > 7.5 }) // Not better than 7.5: [Mad Max, Birdman] movieList.filterNotNullTo(listToFilterInto) // Kinda useless here -
FilterIndexed() movieList.filterIndexed { index, movie -> index.even() } // Even
Indexed: [Mad Max, Birdman, The Silence of the Lambs] -
Map() movieList.map { it.title } // [Mad Max, Dunkirk, Birdman,
Titanik, // The Silence of the Lambs, Finding Nemo] movieList.mapIndexed { index, movie -> index to movie.title } // [(0, Mad Max), (1, Dunkirk), (2, Birdman), // (3, Titanik), (4, The Silence of the Lambs), (5, Finding Nemo)] -
val madMax = Movie("Mad Max", "2015-05-13".toDate(), listOf("action"), 7.3) val dunkirk
= Movie("Dunkirk", "2017-07-19".toDate(), listOf("action", "drama"), 7.6) val birdman = Movie("Birdman", "2014-10-17".toDate(), listOf("drama"), 7.4) val titanic = Movie("Titanik", "1997-12-19".toDate(), listOf("romance"), 7.6) val lamb = Movie("The Silence of the Lambs", "1991-01-30".toDate(), listOf("thriller"), 8.1) val nemo = Movie("Finding Nemo", "2003-05-30".toDate(), listOf("animation"), 7.7) Add some changes -
FlatMap() movieList.map { it.genre } // [[action], [action, drama], [drama],
[romance], [thriller], [animation]] movieList.flatMap { it.genre } // [action, action, drama, drama, romance, thriller, animation] -
Distinct() // [action, action, drama, drama, romance, thriller, animation] genreList.distinct()
// [action, drama, romance, thriller, animation] -
Associate(), AssociateBy() movieList.associate { it.title to it.averageRating } movieList.associateBy({ it.title
}, { it.averageRating }) // {Mad Max=7.3, Dunkirk=7.6, Birdman=7.4, Titanik=7.6, // The Silence of the Lambs=8.1, Finding Nemo=7.7} -
GroupBy() movieList.groupBy({ it.genre }, { it.title }) // {action=[Mad Max,
Dunkirk], drama=[Birdman], romance=[Titanik], // thriller=[The Silence of the Lambs], animation=[Finding Nemo]} -
val average = movieList.filter { it.genre == "action" } .map
{ it.averageRating } .average() // 7.449999999999999 var count = 0 var total = 0.0 for (movie in movieList) { if (movie.genre == "action") { count++ total += movie.averageRating } } val average = total / count // 7.449999999999999 Collection API Old school -
9 lines of old fashioned code 3 lines of neatness
Comparison Slide -
Chart Title Lines 0 2 5 7 9 Java style
Collection API -
val averagePerCategory = movieList .groupBy { it.genre } .mapValues {
it.value .map { it.averageRating } .average() } /* {action=7.449999999999999, drama=7.4, romance=7.6, thriller=8.1, animation=7.7} */ More of Collection API