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 https://agrawalsuneet.github.io/blogs/any-none-all-kotlin/ 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

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 https://agrawalsuneet.github.io/blogs/any-none-all-kotlin/

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.

--

--

--

https://agrawalsuneet.github.io/

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

Recommended from Medium

Compiling a C Program

Coding: a DP problem on Balanced Parenthesis

.Net Assemblies

5 Ways Managed APIs Help Accelerate Business Growth

Rust 101: Structures

Andela Boot-camp Challenges and Adaptability

Fundamentals of AWS (Amazon Web Services)

What’s the best way to learn to code? — Active vs Passive Learning

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

https://agrawalsuneet.github.io/

More from Medium

Introduction to concurrency in iOS

Swift: Error Handling (Do, Try, Catch)

Symmetric cryptography differences between iOS and Android

iOS Dev: Difference between frame and bound, the easiest explanation you can find nowhere