## DEV Community is a community of 554,041 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# How to Initialize an ArrayList in Kotlin

Jeremy Grifski Originally published at therenegadecoder.com on ・4 min read

For those of you familiar with the How to Python series, I thought it would be fun to try my hand at some other languages I’ve been using lately. Today, let’s learn how to initialize an ArrayList in Kotlin.

## Problem Introduction

As someone who came from Java, I often find myself using the ArrayList class to store data. Unfortunately, there’s no clean way of initializing an ArrayList in Java, so I wondered if Kotlin had improved on that issue. For reference, here’s what I don’t want to do:

``````ArrayList<Integer> list = new ArrayList<Integer>()
``````

As you can probably imagine, this solution does not scale well. In fact, I don’t even think it reads well. There’s just too much redundant information. I would prefer to be able to do something like the following:

``````ArrayList<Integer> list = new ArrayList<Integer>(7, -4, 3)
``````

And, for larger data sets, it would be nice to be able to spread the values over multiple lines:

``````ArrayList<Integer> list = new ArrayList<Integer>(
7, -4, 3, 2, 1, 3, 6, 5, 9, 11,
10, 7, -5, -6, 13, 6, -11, 13, 2, 1
)
``````

Unfortunately, that’s just not the case. There are some nasty workarounds, but I was hoping Kotlin would improve on the Java conventions a bit.

## Solutions

Fortunately, Kotlin has improved quite a bit on Java’s verbosity, so I promise there’s a better way to create an ArrayList. It’s just a matter of how.

### Initializing an ArrayList by Brute Force

Naturally, we can translate the Java solution almost directly:

``````val list = ArrayList<Int>()
``````

Here, we’ve created an empty ArrayList of integers. Then, we populated that list one item at a time using the `add()` method.

Of course, we’d love something better than this! Let’s see what Kotlin has to offer.

### Initializing an ArrayList by Conversion

One way to reduce some of the code from above is to create an Array before converting it into an ArrayList:

``````val list = intArrayOf(7, -4, 3).toCollection(ArrayList())
``````

In a single line of code, we’re able to create an array of integers using the ideal syntax. From that array, we can obtain an ArrayList using the `toCollection()` method and passing an empty ArrayList. The `toCollection()` method then populates the ArrayList with all the values in the array.

Obviously, this isn’t ideal as we have to convert between the two types. It would be much nicer to be able to create and initialize an ArrayList directly. Fortunately, we can!

### Initializing an ArrayList with arrayListOf

As it turns out, the collections library includes a function for building an ArrayList in Kotlin directly:

``````val list = arrayListOf<Int>(7, -4, 3)
``````

I’m not totally sure how this method works under the hood, but I imagine it works similar to our brute force solution:

``````fun <T> arrayListOf(vararg elements: T): ArrayList<T> {
val list = ArrayList<T>()
for (element in elements) {
}
return list
}
``````

In any case, we have a concise solution for building up an ArrayList. That’s exciting!

## Performance

Since I got into the habit of measuring performance in Python, I thought I could transition that idea over to Kotlin. Fortunately, there is a standard function which will do just that: `measureNanoTime`. We’ll be using that to test each of the code snippets above:

``````import kotlin.system.measureNanoTime

val bruteForceTime = measureNanoTime {
val list = ArrayList<Int>()
}

val conversionTime = measureNanoTime {
val list = intArrayOf(7, -4, 3).toCollection(ArrayList())
}

val arrayListOfTime = measureNanoTime {
val list = arrayListOf<Int>(7, -4, 3)
}

println("Brute Force: \$bruteForceTime")
println("Conversion: \$conversionTime")
println("ArrayListOf: \$arrayListOfTime")
``````

In my case, the brute force method was significantly faster than the other two methods, but I’m not sure how these functions scale with more input. Feel free to try and let me know. At any rate, here are the results:

Brute Force: 38700

Conversion: 14728800

ArrayListOf: 6319000

For this test, I ended up using `measureNanoTime` over `measureTimeMillis` because I kept getting a value of zero for the brute force method. Now, that makes sense!

For reference, I tested everything on Windows 10 using Android Studio to setup a scratch file called `scratch.kts`. Also, I used Kotlin 1.3.31.

## A Little Recap

As always, here are all the solutions in one convenient place:

``````// Brute force
val list = ArrayList<Int>()

// Array conversion
val list = intArrayOf(7, -4, 3).toCollection(ArrayList())

// Direct method
val list = arrayListOf<Int>(7, -4, 3)
``````

Since I’m just starting to play with Kotlin, this may seem like a pretty trivial tutorial. That said, I guarantee I’ll start digging into more interesting topics. For instance, I’m already excited to start mirroring some of the Python articles.

While you’re here, why not support The Renegade Coder by becoming a member. You’ll get access to all kinds of fun articles like:

Of course, you’re welcome to just hop on the newsletter and decide at a later date. Either way, I appreciate the support.

The post How to Initialize an ArrayList in Kotlin appeared first on The Renegade Coder.