Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Explore Kotlin.Collections API

Kirill
April 24, 2018

Explore Kotlin.Collections API

Kirill

April 24, 2018
Tweet

Other Decks in Education

Transcript

  1. 1. Helps making the code much more concise 2. Part

    of kotlin-stdlib 3. Built on top of Java Collections API Kotlin.Collections highlights
  2. data class Movie( val title: String, val releaseDate: Date, val

    genre: String, val averageRating: Double) Model
  3. 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
  4. List and Mutable List val movieList = listOf(madMax, dunkirk, birdman,

    titanic, lamb, nemo) val movieMutableList = mutableListOf(madMax, dunkirk, birdman, titanic, lamb, nemo)
  5. 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)
  6. MaxBy() movieList.maxBy { it.averageRating } // Max rating: The Silence

    of the Lambs, 8.1 Note: max() if element is a number
  7. Sum() arrayOf(7.3, 7.6, 7.4, 7.6, 8.1, 7.7).sum() // Sum: 45.7

    movieList.sumByDouble { it.averageRating }
  8. First() movieList.first { it.averageRating < 7.5 } // First: Mad

    Max movieList.first { it.averageRating < 3.0 } // NoSuchElementException
  9. Find() movieList.find { it.averageRating < 7.5 } // First: Mad

    Max movieList.find { it.averageRating < 3.0 } // First: null // movieList.find() == movieList.firstOrNull()
  10. Last() movieList.last { it.averageRating < 7.5 } // First: Birdman

    movieList.last { it.averageRating < 3.0 } // NoSuchElementException
  11. FindLast() movieList.findLast { it.averageRating < 7.5 } // First: Birdman

    movieList.findLast { it.averageRating < 3.0 } // First: null // movieList.findLast() == movieList.lastOrNull()
  12. Single() movieList.single { it.averageRating == 7.7 } // Single: Finding

    Nemo movieList.single { it.averageRating == 7.6 } // IllegalArgumentException
  13. SingleOrNull() movieList.singleOrNull { it.averageRating == 7.7 } // Single: Finding

    Nemo movieList.singleOrNull { it.averageRating == 7.6 } // Single: null
  14. Filter() movieList.filter { it.averageRating > 7.5 } // Better than

    7.5: // [Dunkirk, Titanik, The Silence of the Lambs, Finding Nemo]
  15. FilterNot(), FilterNotNull() movieList.filterNot { it.averageRating > 7.5 } // Not

    better than 7.5: [Mad Max, Birdman] movieList.filterNotNull() // Kinda useless here
  16. 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
  17. FilterIndexed() movieList.filterIndexed { index, movie -> index.even() } // Even

    Indexed: [Mad Max, Birdman, The Silence of the Lambs]
  18. 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)]
  19. 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
  20. FlatMap() movieList.map { it.genre } // [[action], [action, drama], [drama],

    [romance], [thriller], [animation]] movieList.flatMap { it.genre } // [action, action, drama, drama, romance, thriller, animation]
  21. 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}
  22. GroupBy() movieList.groupBy({ it.genre }, { it.title }) // {action=[Mad Max,

    Dunkirk], drama=[Birdman], romance=[Titanik], // thriller=[The Silence of the Lambs], animation=[Finding Nemo]}
  23. 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
  24. 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