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
Resources
- Kotlin Thursdays: Crowd-sourced Kotlin Tutorials.
- Kotlin API: The official API docs for any, all, and none.
- Kotlin List API — Part 1: List filtering — the first article in this series.
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 theList
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.
data class Person(val name:String, val age:Int)val people = listOf(
Person("Zander", 78),
Person("Fred", 12),
Person("Ali", 35)
)val noPeople = emptyList<Person>() // empty list
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:
people.any()
// == true, the list is NOT emptynoPeople.any()
// == false, the list is empty
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.
people.any { it.age > 12 }
// == true, Ali and Zander are over the 12people.any { it.age == 35 }
// == true, Ali is 35people.any { it.name == “George” }
// == false, there’s nobody named George.
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
.
noPeople.any { it.age >= 0 }
noPeople.any { true }
noPeople.any { false }
// == false (for all of the above methods)
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.
people.all { it.age > 12 }
// == false, Ali and Zander are over the 12, but Fred is not.people.all { it.age == 35 }
// == false, Ali is 35, but no one else is.people.all { it.age > 2 }
// == true, every person is over the age of 2.
And let’s see how empty lists are handled by the all
method.
noPeople.all { it.age >= 0 }
noPeople.all { true }
noPeople.all { false }
// == true (for all the above methods)
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()
.
people.none()
// == false, the list is NOT emptynoPeople.none()
// == true, the list is empty
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.
people.none { it.age > 12 }
// == false, Ali and Zander are over the 12.people.none { it.age > 0}
// == false, everyone is over the age of 0.people.none { it.age < 6}
// == true, there is no one under age 6.
Using an empty List
was an odd corner case for any
and all
, so we should once again check how that works with none
.
noPeople.none { it.name == “Shadrack” }
noPeople.none { true }
noPeople.none { false }
// == true (for all the above methods)
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 }
— Returnstrue
if at least one element matches the predicate.all { predicate }
— Returnstrue
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 returnsfalse
when called on an empty list.- Both
all { predicate }
andnone { predicate }
always returntrue
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.