Kotlin List API — Part 2: any, all, and none

A straightforward explanation of Kotlin’s methods which check for existence of items in a List

dashfwd
Kotlin Thursdays
5 min readMay 2, 2019

--

Do any of these coffee cups have dragons on them?

Resources

Hi folks! If the text is blurry, make sure the playback settings is at the highest resolution.

Introduction

This article will cover the following methods on Kotlin'sList class:

  • any()
    Does the List have at least one element in it?
  • any { predicate }
    Does the List have at least one element that matches the predicate?
  • all { predicate }
    Does every element in the List match the predicate?
  • none()
    Does the List have zero elements in it?
  • none { predicate }
    Does the List have zero elements that match the predicate?

While this article is covering the List class, it should be noted that all these methods also exist on array type classes as well (IntArray, FloatArray, CharArray, etc.).

Sample Data

To explain how these methods work, let’s start with some sample data using Kotlin’s data class, as we did in Part 1.

We created two lists here; people has three Person elements, and noPeople has zero Person elements (it’s empty).

any()

The any() method can be used to check if a List contains at least one element. Calling people.any() is equivalent to people.size > 0 or people.isNotEmpty(). Why use any() vs the other two? I don’t have a definitive answer for you. I find people.isNotEmpty() a little easier to read, but any() certainly requires fewer keystrokes.

Examples:

any { predicate }

This version of the any method takes as a parameter a lambda which returns true or false (a predicate). This version of the method checks if there is at least one element in the List that matches the predicate.

Let’s see it in action.

Recall that in the people list, Zander is 78, Fred is 12, and Ali is 35.

If you read Part 1, you will notice that this method is functionally equivalent to someList.filter { predicate }.any(). But — in addition to being more typing, using filter would likely be slower because it would first create and populate an entirely new List prior to checking whether there are any elements in the List. In contrast, the any { predicate } method stops processing once it finds the first element that matches the predicate.

Let’s look at how any { predicate} works when you call it on an empty List.

Interestingly, when you call any { predicate } on an empty List, it always returns false. It’s an odd little corner case but it’s good to be aware of it.

all { predicate }

The method checks if every element in the List that matches the predicate. Contrast this with any { predicate } which only requires that one element matches the predicate.

Let’s dive into code again.

Recall that in the people list, Zander is 78, Fred is 12, and Ali is 35.

And let’s see how empty lists are handled by the all method.

Once again, we have an interesting special case. The all method always returns true when called on an empty List.

none()

The none() method can be used to check if a List contains zero elements. Calling people.none() is equivalent to people.size == 0 or people.isEmpty().

none { predicate }

The method checks if zero elements in the List that match the predicate.

Back to the code examples… Recall that in the people list, Zander is 78, Fred is 12, and Ali is 35.

Using an empty List was an odd corner case for any and all, so we should once again check how that works with none.

Here, the none method always returns true when called on an empty List.

Summary

In this article, I’ve covered the Kotlin List methods any, all, and none.

Of those, these two can be called without any parameters:

  • any() — returns true if the List is not empty.
  • none() — returns true if the List is empty.

There are also methods that take a lambda as a parameter, and they are:

  • any { predicate } — Returns true if at least one element matches the predicate.
  • all { predicate } — Returns true if every element matches the predicate.
  • none { predicate } — Returns true if zero elements match the predicate.

There are also the following special cases to be aware of.

  • any { predicate } always returns false when called on an empty list.
  • Both all { predicate } and none { predicate } always return true when called on an empty list.

Thanks for reading! I’ll see you soon for Part 3, where I will cover List mapping.

Questions, feedback? Find me on Twitter.

--

--