Slide 1

Slide 1 text

WELCOME KOTLIN / Everywhere Nairobi Study Jam 3 / 6

Slide 2

Slide 2 text

Expressive & Efficient Collection Processing in Kotlin HARUN WANGEREKA ANDROID DEVELOPER, APPS:LAB

Slide 3

Slide 3 text

Agenda ● Basics ● Collections Playground ● Summary

Slide 4

Slide 4 text

Heavily inspired by Scala

Slide 5

Slide 5 text

Built on top of Java Collections API

Slide 6

Slide 6 text

Defines collections in the kotlin.collections package

Slide 7

Slide 7 text

public interface Iterable { public operator fun iterator(): Iterator } Basis for all collections

Slide 8

Slide 8 text

Iterator object knows how to iterate over a collection public interface Iterator { public operator fun next(): T public operator fun hasNext(): Boolean }

Slide 9

Slide 9 text

Next on hierarchy is Collection public interface Collection : Iterable { public val size: Int public fun isEmpty(): Boolean public operator fun contains(element: @UnsafeVariance E): Boolean override fun iterator(): Iterator public fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean }

Slide 10

Slide 10 text

Next hierarchy is Collection ● List ● Set ● Map

Slide 11

Slide 11 text

Collection Mutable Read-Only ● MutableList ● MutableSet ● MutableMap ● List ● Set ● Map

Slide 12

Slide 12 text

Collections Playground

Slide 13

Slide 13 text

Inspired by the “collector”

Slide 14

Slide 14 text

When the speaker is ready, the collector will appear

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Summary ● Create - We have mutable and read-only ● Operators - Functional operations plus,minus, plus assign and minus assign ● Transformers - build new collections based on conditions provided ● Aggregators - operations that return single value based on collection content ● Searching - getting particular elements ● Filtering - filter-in with some predicate/condition ● Actions - [action] on a particular iterable

Slide 17

Slide 17 text

● Retrieving Collections Parts - Retrieves parts of collections - ● Retrieving Single Elements ● Collection Ordering ● Collections write operations - add(),remove() ● List/Set/Map Specific Operations

Slide 18

Slide 18 text

Neat Stuff. The cool way

Slide 19

Slide 19 text

..the old way var count = 0 var total = 0.0 for (album in albumList){ if (album.genre == "HipHop"){ count ++ total += album.averageRating } } val average = total/ count average.print()

Slide 20

Slide 20 text

The collections.way val avg = albumList.filter { it.genre == "HipHop" } .map { it.averageRating } .average() .print()

Slide 21

Slide 21 text

..the old way val speakersList :List = getSpeakersInfo() val sessionList : List = snapshots.toObjects() sessionList.map { sessionModel -> val sessionSpeakerIdList = sessionModel.speaker_id sessionSpeakerIdList.forEach { speakerId -> speakersList.forEach {speakerModel -> if (sessionSpeakerIdList.contains(speakerModel.id)){ sessionModel.speakerList.add(speakerModel) } } } }

Slide 22

Slide 22 text

The collections.way val speakerById : Map = getSpeakerInfo().associateBy { it.id} val sessionList : List = snapshots.toObjects() sessionList.onEach { sessionsModel -> sessionsModel.speaker_id.mapNotNull { speakerById[it]?.let { speakerModel -> sessionsModel.speakerList.add(speakerModel) } } }

Slide 23

Slide 23 text

articlesList.filter { it == "Kotlin Collections" }

Slide 24

Slide 24 text

Resources ● http://jussi.hallila.com/Kollections/ ● https://antonioleiva.com/collection-operations-kotlin/ ● https://kotlinlang.org/docs/reference/collections-overview.html ● https://github.com/wangerekaharun/CollectionsPlayGround