Перейти к содержимому

collect(), first(), last(), single, toSet, toList, fold, reduce | Terminal operators in Kotlin Flow

Lukas Lechner

0:00 / 0:00

collect(), first(), last(), single, toSet, toList, fold, reduce | Terminal operators in Kotlin Flow

2 237 просмотров · 3 г. назад
Lukas Lechner
2,54 тыс. подписчиков
2 237 просмотров · 3 г. назад
🎓 Get the full 15+ hour-long Course "Kotlin Coroutines and Flow for Android Development" with a nice discount here: https://www.lukaslechner.com/coroutin... 👨‍🎓 Here is the link to the Github Example Repository: https://github.com/LukasLechnerDev/Ko... We need to use terminal operators in order to execute a flow and to receive the values that the flow emits. These terminal operators are covered: collect {} - receive all emissions of the flow first() - returns only the first emission of the flow last() - returns only the last emission of the flow single() - returns single emission of a flow, but only if the flow only emits a single value, Otherwise - if the flow emits no values or multiple values - an Illegal state Exception is thrown toList(), toSet() - operators wait until the flow completes and then all the emissions will be returned as a list or set fold(), reduce() - to perform operations like summing up all of the emitted values of a flow TimeStamps 00:00 - Course Info 00:40 - Flow vs Collections 03:15 - collect() 07:10 - first() 09:35 - last() 10:30 - single() 11:40 - toList(), toSet() 13:00 - fold() 15:35 - reduce() 15:50 - Summary ➤ Subscribe to the channel:    / @lukaslechner   🌎 Check out my website: https://www.lukaslechner.com 📬 Subscribe to my newsletter: https://www.lukaslechner.com/newsletter/ ➤ Follow me on twitter:   / lukaslechnerdev