Checking existence –
contains(element): –It is used to check if an element is in the list or not. It returns true if element is there else it returns false.
containsAll(elements[]): – It is used to check if all the elements are in the list or not. It returns true if all the elements are there else it returns false.
in keyword: – It is used to check if all the elements are in the list or not. It returns true if the element are there else it returns false.
isEmpty(): – It is used to check if the list is empty or not. It returns true if the elements are not in list else it returns false.
isNotEmpty(): – It is used to check if the list is empty or not. It returns true if the elements are in list else it returns false.
Kotlin program of checking existence of string in a list –
Java
fun main(args: Array<String>) { val list=listOf( "Geeks" , "for" , "Geek" , "A" , "Computer" , "Science" , "Portal" ) //checking the string is in list or not println(list.contains( "For" )) println(list.containsAll(listOf( "A" , "Computer" ))) println(list.isEmpty()) println(list.isNotEmpty()) } |
Output:
false true false true
Retrieve Single Elements In Kotlin
Kotlin Collections allows to retrieve single elements with respect to different conditions. It totally depends on what kind of Collection is used to store the data. The elements of a collection can be retrieved by position, condition, randomization and with respect to the existence of that element in the list.