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. 
 

Similar Reads

Retrieving by Position –

elementAt(index): – elementAt() takes index of the element and retrieve it from the list. The index of the list starts from 0 and goes till n-1 where n is no of elements within the list. It is the most simple and easiest way to retrieve the element and is only preferred when the position of the needed element is known in advance.Kotlin program of using elementAt() method –...

Retrieving by condition –

...

Random Retrieving –

...

Checking existence –

...