SlideShare a Scribd company logo
Kotlin for Android
May 24, 2018
y/kotlin-workbook
Kotlin Collection APIs
y/kotlin-workbook
all, none
COLLECTIONS
fun <T> Iterable<T>.all(
predicate: (T) -> Boolean
): Boolean
fun <T> Iterable<T>.none(
predicate: (T) -> Boolean
): Boolean
y/kotlin-workbook
● Checks if all values in a collection evaluate correctly
● all breaks at the first false
● none breaks at the first true
any, find, single
COLLECTIONS
fun <T> Iterable<T>.any(
predicate: (T) -> Boolean
): Boolean
y/kotlin-workbook
● Checks if any values in a collection evaluate to true
● any and find break at the first true
● single throws exception if 0 or more than 1 element
match
● find and single return the element that matched
asList
toList
toSet
...
COLLECTIONS
fun <T, C : MutableCollection<in T>>
any_collection<out T>.toCollection(
destination: C
): C
y/kotlin-workbook
● Converts one collection to another
associate
associateBy
associateByTo
COLLECTIONS
fun <T, K, V> Iterable<T>.associate(
transform: (T) -> Pair<K, V>
): Map<K, V> {}
fun <T, K> Iterable<T>.associateBy(
keySelector: (T) -> K
): Map<K, T> {}
fun <T, K, V> Iterable<T>.associateBy(
keySelector: (T) -> K,
valueTransform: (T) -> V
): Map<K, V> {}
y/kotlin-workbook
● Map a collection
component1-5
COLLECTIONS
val pixel = arrayOf(0, 5, 10, 25)
val (_, r, _, b) = pixel
val hasYelpPotential = pixel.let { (a, r) ->
a == 0xff && r == 0xd3
}
y/kotlin-workbook
● Destructing
● Can only be used to initialize, but not assign
val day = 1..24
val has5 = 5 in day || day.contains(5)
val no30 = 30 !in day || !day.contains(30)
contains
in
COLLECTIONS
y/kotlin-workbook
● Prefer in over contains
filter***
COLLECTIONS
val day = 1..24
val workingHours = day.filter { it in 9..17 }
val workingHours = day.filter((9..17)::contains)
y/kotlin-workbook
● A whole family of fuctions
● Create a subset of the original collection
● Can transform to antoher collection type
distinct
COLLECTIONS
y/kotlin-workbook
● Like toSet(), but returns a List (maintaining the order
of elements)
drop, take
COLLECTIONS
val a = arrayOf(-1, 0, 1, 2)
a.drop(2) == a.dropWhile { it <= 0 } // [1, 2]
a.take(2) == a.takeWhile { it <= 0 } // [-1, 0]
y/kotlin-workbook
● drop to skip a few elements
● take to drop the rest
flatMap
flatten
COLLECTIONS
fun <T, R> Iterable<T>.flatMap(
transform: (T) -> Iterable<R>
): List<R>
fun <T> Iterable<Iterable<T>>.flatten(): List<T>
y/kotlin-workbook
● Flatten a collection of composite objects to a list
fold, reduce
COLLECTIONS
fun <T, R> Iterable<T>.fold(
initial: R,
operation: (acc: R, T) -> R
): R
val day = 1..24
day.fold("") { acc, next ->
"$acc,$next"
}
y/kotlin-workbook
● Flatten a collection of objects to a single object
forEach
onEach
COLLECTIONS
y/kotlin-workbook
● Replacement for a for loop
● onEach returns the collection after the loop has been
evaluated
joinTo
joinToString
COLLECTIONS
fun <T, A: Appendable> Iterable<T>.joinTo(
buffer: A,
separator: CharSequence = ", ",
prefix: CharSequence = "",
postfix: CharSequence = "",
limit: Int = -1,
truncated: CharSequence = "...",
transform: (T) -> CharSequence = null
): A
joinToString(...) = joinTo(StringBuilder()).toString()
(1..24).joinToString()
y/kotlin-workbook
● Comma separation out-of-the box
● Quite configurable
min, max, average
COLLECTIONS
y/kotlin-workbook
● As you’d expect
plus, minus
COLLECTIONS
y/kotlin-workbook
● As you’d expect
orEmtpy
COLLECTIONS
val maybe: List<String>? = null
maybe.joinToString() // Compile Error
maybe.orEmpty().joinToString() // Empty Non-Null String
y/kotlin-workbook
● Extension on nullable collections
● Returns empty collection instead of null
reverse
COLLECTIONS
y/kotlin-workbook
● Reverses the order of elements
zip, unzip
COLLECTIONS
y/kotlin-workbook
● zip 2 collections into a List<Pair>
● unzip a list of Pairs into a Pair of lists
● zip into a custom composite - unzip it via associate
Questions?
Next Up: Custom DSLs

More Related Content

PPTX
Functional Programming Concepts for Imperative Programmers
PDF
Functional Programming in JAVA 8
PPTX
2.3 graphs of functions
PDF
The Ring programming language version 1.2 book - Part 21 of 84
PPT
Stacks queues
PPT
Heaps & Adaptable priority Queues
PPT
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
PPT
Maps&hash tables
Functional Programming Concepts for Imperative Programmers
Functional Programming in JAVA 8
2.3 graphs of functions
The Ring programming language version 1.2 book - Part 21 of 84
Stacks queues
Heaps & Adaptable priority Queues
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Maps&hash tables

What's hot (9)

PPTX
Lecture 6 data structures and algorithms
PPSX
Stacks fundamentals
PDF
FP in scalaで鍛える関数型脳
PDF
Data structures stacks
PDF
Programación funcional con Haskell
PDF
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
PPTX
Application of Stack - Yadraj Meena
PDF
The Ring programming language version 1.5.2 book - Part 31 of 181
PPT
Savitch ch 18
Lecture 6 data structures and algorithms
Stacks fundamentals
FP in scalaで鍛える関数型脳
Data structures stacks
Programación funcional con Haskell
[Expert Fridays] Александр Чичигин - Как перестать бояться и полюбить COQ
Application of Stack - Yadraj Meena
The Ring programming language version 1.5.2 book - Part 31 of 181
Savitch ch 18
Ad

Similar to Kotlin For Android - Collections APIs (part 6 of 7) (20)

PPTX
Kotlin Collections
PDF
Kotlin 101 Workshop
PDF
Kotlin functional programming basic@Kotlin TW study group
PPTX
Scala collections wizardry - Scalapeño
PDF
Compose Camp session 4.pptx.pdf
PDF
Scala Collections
PDF
Scala collections
PPTX
K is for Kotlin
PPTX
Compose 3rd session.pptx
PDF
ANDROID 4.pdf
PPTX
The Arrow Library in Kotlin
PPTX
3 Презентация Kotlin - why not?
PDF
EMFPath
ODP
Collections In Scala
PDF
Scala collections api expressivity and brevity upgrade from java
PPTX
Kotlin collections
PDF
Kotlin - scope functions and collections
PDF
Will it Blend? - ScalaSyd February 2015
PDF
Privet Kotlin (Windy City DevFest)
PDF
Collections
Kotlin Collections
Kotlin 101 Workshop
Kotlin functional programming basic@Kotlin TW study group
Scala collections wizardry - Scalapeño
Compose Camp session 4.pptx.pdf
Scala Collections
Scala collections
K is for Kotlin
Compose 3rd session.pptx
ANDROID 4.pdf
The Arrow Library in Kotlin
3 Презентация Kotlin - why not?
EMFPath
Collections In Scala
Scala collections api expressivity and brevity upgrade from java
Kotlin collections
Kotlin - scope functions and collections
Will it Blend? - ScalaSyd February 2015
Privet Kotlin (Windy City DevFest)
Collections
Ad

More from Gesh Markov (6)

PPTX
Kotlin For Android - How to Build DSLs (part 7 of 7)
PPTX
Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)
PPTX
Kotlin For Android - Properties (part 4 of 7)
PPTX
Kotlin For Android - Functions (part 3 of 7)
PPTX
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
PPTX
Kotlin For Android - Basics (part 1 of 7)
Kotlin For Android - How to Build DSLs (part 7 of 7)
Kotlin For Android - Useful Kotlin Standard Functions (part 5 of 7)
Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Functions (part 3 of 7)
Kotlin For Android - Constructors and Control Flow (part 2 of 7)
Kotlin For Android - Basics (part 1 of 7)

Recently uploaded (20)

PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
assetexplorer- product-overview - presentation
PDF
Nekopoi APK 2025 free lastest update
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
top salesforce developer skills in 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Design an Analysis of Algorithms II-SECS-1021-03
System and Network Administraation Chapter 3
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
assetexplorer- product-overview - presentation
Nekopoi APK 2025 free lastest update
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Odoo Companies in India – Driving Business Transformation.pdf
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
history of c programming in notes for students .pptx
Design an Analysis of Algorithms I-SECS-1021-03
top salesforce developer skills in 2025.pdf
Digital Strategies for Manufacturing Companies
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf

Kotlin For Android - Collections APIs (part 6 of 7)

Editor's Notes

  • #3: Add a slide about asSequence() Mark terminal vs transfomation functions Callout that all of these are extension functions
  • #8: Not limited to interables Auto-generated for data classes - so choose your first 5 constructor arguments carefully
  • #10: Be mindful of allocations inside your looped functions
  • #13: Example a list of businesses, each business has a list of phone numbers -> a list of phone numbers
  • #21: ALMOST END OF SLIDES - NEXT SLIDE IS LAST - Q&A STARTS