also. Less is written about what comes with the
ranges, and other packages of the standard library. In this article we'll explore them.
Kotlin comes with
Triple which are basic generic tuples:
Pair comes with an useful
which lets us create one like this:
Then we can use this syntax to create
Maps in a much more readable way:
If you have worked with the Kotlin stdlib for a while you probably bumped into a bunch of library functions which
are either improvements over the Java versions or new additions by Kotlin.
reduce for example which are defined on
Iterable objects and there are a bunch of others which are defined on immutable
Note that from now on we’ll talk about operations which are defined for immutable collections.
Creating collections never have been easier. We have
mapOf and even
arrayOf to create the
What’s interesting is that most of these operations are also defined for
Note that a
Mapin Kotlin does not implement the
Collectioninterface, but defines some operations for
Maps which are counterparts for the ones in
Maps as collections of
Pairs and we can create
Maps from any collection which holds them:
In addition to
reduce we also have
mapKeys will create new
Maps when we call them. They are useful when we have a
Map and we only
want to transform either the keys or the values. The
filter variants follow the same logic but with filtering.
We can also combine them:
If we want to perform operations other than these we can turn our
Map into a collection by calling
In addition to
toMap and other conversion functions there are also some specialized ones which are defined
on only some selected types like
Char. For example we can turn a
Bytes to a
There is a
to*Array defined for each primitive type. They all return a corresponding
*Array type which is optimized.
Immutable collections are perfect for functional programming since every operation defined on them returns a new version
of the old collection without changing it.
This also means that they are safe from a concurrency perspective since we don’t need locks to work with them.
A problem though is that we lose operations like
Luckily most operations which work with mutable collections have an immutable counterpart.
minus work like
remove and we also have
intersect. They work like
We can also work with collections in a way offset and limit works in RDBMSes.
drop will return with a
without the first
dropLast works in the same way but drops elements from the end:
There is also
dropLastWhile which drops elements until a certain condition is met:
For all of the above functions there is a
take variant which works like
drop but it takes elements:
These are useful but sometimes we only want to pick distinct values. We can do so by calling
distinctBy we can write our own selector function:
We’ve already seen ways we can turn
Lists but can we do it the other way around? The answer is yes.
Kotlin comes with
associateBy which lets us split our
Lists in different ways.
groupBy separates our
List into multiple
Lists grouped by keys, so the result is a multimap.
We only need to provide a key selector function to do so. In this example we group a
even and odd groups:
associate is different in a way that it transforms each element to a key-value pair and if multiple values map to the same key
only the last one is returned. In our previous list of
Ints which are sorted this will effectively give us the greatest
odd and even numbers:
A variant to
associateBy which does not transform the original values but takes a key selector
function. If multiple elements would have the same key only the last one is added to the resulting
Map. This is
an example which does the same as the previous one but with
partition is a special transformation function which groups to only a
Lists based on the result of
We’ve seen how we can split things but let’s see what we have for joining them!
zip will work exactly like the zipper of your trousers: it zips two
Lists into a
We can be a bit more sophisticated if we provide a transform function to
zipWithNext will pair each element with the next:
and it can also take a transform function:
Sometimes we want to transform our collections to a
String representation. This is useful if we want to log
the contents of them for example. For this purpose we have
joinTo which takes an
Appendable and some extra
arguments (like a separator) and returns the
Appendable with the contents of the collection appended:
Since joining to a
String is so common we also have
The examples above are far from exhaustive but there are some interesting functions which are really useful sometimes.
For example we have the most commonly used
String transformations as extension functions:
There are also
Ranges which are very useful for iteration. We can create them directly from numbers with
A real life example
These code snippets are nice but you might be wondering how a real life use case looks like. Let’s see how we can process some log information with what we have learned so far:
We’ve only scratched the surface with the examples above but I hope that you now have an idea about what the Kotlin stdlib has to offer. I strongly encourage you to open your IDE and take a look at these functions from the source. They are documented very well so you can get started in no time.