First N Elements In Kotlin » The Bored Dev

First N Elements in Kotlin

In this short article we’ll take a look at how can we get the first or last n elements in Kotlin. Let’s see the different ways in which we can achieve that.

First n elements from a list - Kotlin
Photo by John Cameron on Unsplash

Kotlin take()

The first method we’re going to look at is take, we can use this method when we need to take just the first n elements. For example, we’re taking the first five elements in our test below.

    @Test
    fun `should take first five elements`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.take(5)

        assertThat(filtered).containsExactly(10, 1, 7, 99, 24)
    }

This example is quite straightforward so probably it doesn’t need further explanation. What if we need to take the last n elements?

takeLast

In the same way that we can use take(n) we also have takeLast(n) available. Let’s see how that code would look like:

    @Test
    fun `should take last five elements`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.take(5)

        assertThat(filtered).containsExactly(24, 10, 7, 3, 33)
    }

Looks quite easy, right?

What if we want to exclude the first or last elements instead? Kotlin does provide a way to easily do that as well!

drop

In the same way that take(n) takes the first n elements, drop(n) will exclude the first n elements and return the remaining part of the list.

    @Test
    fun `should drop first five elements`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.drop(5)

        assertThat(filtered).containsExactly(10, 7, 3, 33)
    }

That’s exactly what we’d expect! As always, Kotlin methods are quite readable and easy to reason about.

If we wanted to exclude them from the tail of the collection? How would that look like?

dropLast

As you would expect, this method works exactly in the same way as its opposite method, takeLast(n).

Let’s look at example with a unit test.

    @Test
    fun `should drop last five elements`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.dropLast(5)

        assertThat(filtered).containsExactly(10, 1, 7, 99)
    }

As expected, the method excludes the last five elements from our initial list.

There’s another way to filter elements by position, they can be used to achieve something similar to take/drop methods.

filterIndexed

Although we don’t think these methods are that useful or used regularly, we’ll show how it works.

The method filterIndexed works exactly as filter method, with the only difference being an additional function parameter that contains the index for each element.

We could implement the same functionality as take and drop using this method.

    @Test
    fun `should take first five elements using filter indexed`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.filterIndexed { index, _ ->  index <= 4}

        assertThat(filtered).containsExactly(10, 1, 7, 99, 24)
    }

    @Test
    fun `should drop first five elements using filter indexed`() {
        val numbers = listOf(10, 1, 7, 99, 24, 10, 7, 3, 33)

        val filtered = numbers.filterIndexed { index, _ ->  index > 4}

        assertThat(filtered).containsExactly(10, 7, 3, 33)
    }

We have implemented both take and drop by checking that the index is below or above 4. We have to remember that Kotlin collections indexes start from zero, that’s why we check for index 4 in the boundaries.

This example is being shown just for learning purposes, no one would normally decide to use any of these methods considering that we can use take or drop instead.

We could also use filterIndexedTo, which appends the results to an existing collection received as an argument

If you are interested in knowing more about how to filter elements from collections in Kotlin, you could also read our article “How To Filter Collection in Kotlin“.

If you are interested in knowing about Kotlin in more detail, we recommend the following books for your own reading:

Conclusion

We’ve seen how we can make use of take, drop or even filterIndexed methods to be able to filter elements by their position in the collection.

Kotlin provides some additional methods compared to Java with regards to processing data in collections, this is due to the power that Kotlin extensions bring into the language.

We hope you’ve found this article useful and we’re hoping to see you back very soon!

One comment