Andrew
Andrew

Reputation: 4702

How to check if all of multiple values met one or another condition

I would like to check, if all of my variables are either null/other condition or not null/other condition. If some are null and some are not, I would like to return those variables which are null. For example:

val value1 = null
val value2 = null
val value3 = null
val value4 = "Toast"

if(value1 != null && value2 != null && value3 != null && value4 != null) return true 
else if (????) return value that are null

Maybe I am just brain afk and the solution is easier than I think it is.

Upvotes: 0

Views: 545

Answers (1)

Carlos
Carlos

Reputation: 6021

Jam the values into a list and filter them?

val mylist = mapOf(value1, value2, value3, value4)
val notnulls = mylist.filter{it != null}

Now notnulls will have the values that aren't null.

I'm pretty sure there's an any{...} as well in there, same as counting whether there's anything left after your filter.

Upvotes: 3

Related Questions