any(), none() & all() : Kotlin

Kotlin is a powerful language that reduces a lot of boilerplate code required to perform basic operations in comparison to Java. The classic examples for the same are any, non and all functions which were added to the Iterable interface and Map interface.

This post was originally posted at and reposted on Medium on 28th April 2021.

Let try to understand what do they do, why they are required and when to use them. But before we begin, I am assuming a basic knowledge of Map, Set and List.

The List provides the functionality to maintain the ordered collection. whereas Set and Map don’t provide the ordering. Both of them provides uniqueness but Map is a key-value pair mapping.

In short, all three of them are used to store data/objects of similar types.

Since the collection is involved, filtering will be required to iterate the data faster.
To make this filter easier, Kotlin has added a few functions as an extension function to Iterable as well as Map.

Let look at them one by one.


Any is a function that is added as an extension to Iterable and Map interfaces, which take a higher-order function as param to predicate the condition and return Boolean as true, if any of the items in List, Set or Map confirms that condition, else return false.

val list = listOf("one", "two", "three", "four")
val set = setOf("one", "two", "three", "four")
val map = mapOf(1 to "one", 2 to "two", 3 to "three", 4 to "four")

println(list.any { it.endsWith("e") }) // true
println(set.any { it.endsWith("e") }) // true
println(map.any { it.value.endsWith("e") }) // true

Please continue reading at

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to hide API key in Ruby

Cars… S01E01 — Pilot

Copper Cable Jointer Course — Milcom

Iteration to Recursion: The Collatz Conjecture (Swift 4)

Navigating Programming for the NewBie

Yield Farming Completed, What now?

Best 5 Web Development Courses in Udemy 2020

Analyzing stock price time series with modern Fortran, Part 3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

More from Medium

Screen templates — using Compose, SwiftUI & KMM

How to Retry Network Requests Using Swift’s Combine

@State Property Wrapper Explained

[LeetCode] 2114. Maximum Number of Words Found in Sentences (Swift)