Kotlin Game changer on Collections
There are many features in Kotlin like optional type system that makes is in a different level in comparison with Java. There are still many hidden features in Kotlin that mainly don’t catch by the developer in daily Katlin programming unless you are really into performance and under hood things:).
Recently I was engaged with one app that should deal with pretty big data from the back-end which needs traverse them and does many modifications on them. Well, why back-end API not doing it? I can connect you with project manager and please share with me the answer if you found it:).
I am a fanboy of Rxjava. It brings up an infinite power for you. I always do all my collection modifications through it until I deeply start working on a collection in Kotlin and stopped by Map and Filter. There is no need talk about how they are working and how should implement them.
just a quick example,
You are right and nothing different from Map and Filter among Rxjava and Java and Kotlin. Sorry, we are all wrong.
Kotlin has hidden tune behind the scene which makes modification over collection much faster. This advantage will be a game changer while you are struggling with a big size of the collection which come back from API call.
Lazy Collection, knowns as “Sequences”, before any explanation let’s have a look at the following picture:
The Eager method goes over the collection the map then filter and The Lazy process element one by one for map and filter.
Eager calculation make temporary collections eagerly, meaning the intermediate result of each step is stored in a temporary list.
The Sequences method picks an another way to perform such computations which wont need to have any intermediate list.
We know that Java 8 has Stream that following the same concept and kotlin does its own version of it. Well, we can’t use Java 8 on Android as you know.
The sequence simply implemented in code like the following example:
I totally recommend running some simple unit test on Sequences vs normal collection -> Kotlin is awesome.