Kotlin - Idiomatic way to check array contains value Kotlin - Idiomatic way to check array contains value arrays arrays

Kotlin - Idiomatic way to check array contains value


The equivalent you are looking for is the contains operator.

array.contains("value") 

Kotlin offer an alternative infix notation for this operator:

"value" in array

It's the same function called behind the scene, but since infix notation isn't found in Java we could say that in is the most idiomatic way.


You could also check if the array contains an object with some specific field to compare with using any()

listOfObjects.any{ object -> object.fieldxyz == value_to_compare_here }


Using in operator is an idiomatic way to do that.

val contains = "a" in arrayOf("a", "b", "c")