Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: check if list has an object of a specific type

I have an interface:

interface Change

and a function:

 fun test (listOfChanges: List<Change>)

and this class that implements Change interface:

 class MyChange :Change

How to check if listOfChanges has an element of MyChange class?

Like: if(listOfchanges.contains(MyChange::class)

like image 726
Mukhamedali Zhadigerov Avatar asked Dec 08 '22 11:12

Mukhamedali Zhadigerov


1 Answers

You can use any():

listOfChanges.any { it is MyChange }
like image 81
jsamol Avatar answered Dec 11 '22 09:12

jsamol