How to create Maps in Kotlin using 5 different factory functions

Maps are a key collection type in most languages out there, so the ease of using them should be a given.

In Kotlin, the collection API has been carefully created to make our job easier when working with collections. The syntax is concise, but still readable, and a huge set of helpful functions is available.

In the coming weeks I'll post a few articles on how you can create and interact with the different collections.

Let's start by looking at 5 factory functions we can use to create maps in Kotlin.

1. mapOf — creating an immutable map

The first and most standard way of creating a map in Kotlin is by using mapOf.

mapOf("name" to "Luke", "age" to "23")  
//> {name=Luke, age=23}

mapOf creates an immutable map of the key-value pairs we provide as parameters.

Since the map is immutable, we won't find methods like put, remove or any other modifying functions. However, read-only functions like get or containsKey are available.

You'll also find a huge amount of helpful extension functions that enables us to do typical functional operations like map, filter and many more! I'll get back to these functions in a later post.

2. mutableMapOf — creating a mutable map

Now if we truly need a mutable map that we can modify, we can use mutableMapOf.

mutableMapOf("name" to "Luke", "age" to "23")  
//> {name=Luke, age=23}

In this map we got some mutable functions like put, remove and clear available in addition to the functions available for the immutable map.

val person = mutableMapOf("name" to "Luke", "age" to "23")  
//> {name=Luke, age=23}

person.put("location","Oslo")  
//> {name=Luke, age=23, location=Oslo}

person.remove("age")  
//> {name=Luke, location=Oslo}

person.clear()  
//> {}

With that said, we still have the extension functions that also were available for the immutable map.

This means for instance if you use the extension function filter, it'll not change the original map in a mutable way, but instead create a new one based on the result.

val person = mutableMapOf("name" to "Luke", "age" to "23")  
val onlyName = t.filter { it.key == "name" }

println(person) // All data is still in the original map  
//> {name=Luke, age=23}

println(onlyName) // onlyName is a new map with the filtered name property  
//> {name=Luke}

Now that we've covered the standard immutable and mutable Kotlin Map, let's look at a three Java maps that have a special factory function in Kotlin.

Helpful factory functions for creating Java Maps.

Looking at Java, we got a range of different Map implementations, and we can use them all in Kotlin.

For three of these Java maps, Kotlin provides similar factory functions as for the maps we looked at earlier in this post.

This means less boiler plate code!🎉

3. sortedMapOf — creating a SortedMap

The first Java map we're going to look at is the SortedMap.

A SortedMap is simply a map that is ordered by its keys.

sortedMapOf("name" to "Luke", "age" to "23", "location" to "Oslo")  
//> {age=23, location=Oslo, name=Luke}

As you can see, we got the same concise syntax in this method as with the Kotlin maps.

Now, it's worth mentioning that since SortedMap is a typical Java map, it isn't immutable.

So nothing would stop us from doing mutable operation like put, remove or any other mutable operation available in Javas Map interface.

val person = sortedMapOf("name" to "Luke", "age" to "23", "location" to "Oslo")  
person.put("birthday", "1995-01-01")  
//> {age=23, birthday=1995-01-01, location=Oslo, name=Luke}

4. hashMapOf — creating a HashMap

Another Map implementation that Kotlin got a factory function for is the HashMap — which is hash table based implementation.

hashMapOf("name" to "Luke", "age" to "23", "location" to "Oslo")  
//> {name=Luke, age=23, location=Oslo}

Again, this is an implementation from Java, so the mutable methods and behavior are present.

5. linkedMapOf — creating a LinkedHashMap

The last factory function we're going to look at is the linkedMapOf.

This function we'll create LinkedHashMap, which is another variant of a hash table implementation.

linkedMapOf("name" to "Luke", "age" to "23", "location" to "Oslo")  
//> {name=Luke, age=23, location=Oslo}

There you go — 5 factory functions that help us create Maps in a clean and concise way!