In this article, we will like to talk about some great lambda functions that kotlin provide us. These function can help us deal with complex data structures making code cleaner and better. As more and more developers are starting to move to kotlin this can also act as a small cheat sheet for them.

One of the great feature kotlin provides us is passing a function as a parameter to other functions or storing function in a variable. Let’s talk about some function that kotlin collection provides us.

Let’s define some basic data structure we will be using in this article. We will try to make it a question to answer discussion. All codes are tested on https://pl.kotl.in/u_Z2pyAtJ

data class User(val name:String,val age:Int) val list = listOf<User>(

User("Yash",24),

User("Dilpreet",22),

User("himanshu",24),

User("deepanshu",21),

User("rohit",19),

User("sonam",18)

)

Q1. Find if any user has age greater than 22?

One common way is to loop through the list and check if any user has age greater than 22 but why to write so much of code if kotlin has already done this for us.

any — returns true if any element in the collection satisfies the predicate

val result = list.any{item->item.age>22} // result = True

What if we need to find if all user has age greater than 22?

all — returns true if all element satisfies the predicate

val result = list.all{item->item.age>22} // result = False

If we need to check if no element in the list satisfies the predicate.

none — returns true if no element satisfies the given condition

val result = list.none{item->item.age>22}

We see how easy and clean it is. Now let’s try some more interesting one.