= 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
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)
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
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)]
{ 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