Open In App

Retrieve Single Elements In Kotlin

Last Updated : 15 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In Kotlin, we can easily retrieve single elements from a collection in many ways depending on our need—by position, by condition, randomly, or by checking if the element exists. The method we choose depends on the type of collection (like List, Set, etc.) and the situation.

1. Retrieving by Position

If we know the exact position (index) of an element in a list, we can directly retrieve it.

a). 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.

Example:

Kotlin
fun main() {
    val list = listOf("GeeksforGeeks", "Kotlin", "Portal")
    println(list.elementAt(0)) // prints the first element
}

Output: 

GeeksforGeeks


b). Safe ways to retrieve by index

If the index might be invalid (e.g., larger than list size), Kotlin provides safe methods to avoid exceptions.

  • elementAtOrElse(index) { lambda } - If index is invalid, lambda block is executed.
  • elementAtOrNull(index) - Returns null if index is out of bounds.

Example:

Kotlin
fun main() {
    val list = listOf("GeeksforGeeks", "Kotlin", "Portal")
    println(list.elementAtOrElse(5) { "Element index not found" })
    println(list.elementAtOrNull(5)) // returns null
}

Output: 

Element index not found
null


2. Retrieving First and Last Elements

We can also retrieve first and last element from the list below two methods - 

  • first(): - It is used to retrieve the first element from the list i.e having the index 0. 
  • last(): - It is used to retrieve the last element of the list i.e having the index n-1 where n is the size of the list.

Example:

Kotlin
fun main() {
    val list = listOf("GeeksforGeeks", "Kotlin", "Portal")
    println(list.first()) // First element
    println(list.last())  // Last element
}

Output: 

GeeksforGeeks
Portal


3. Retrieving by Condition (Predicate) -

Using first() and last() method, we can retrieve the elements matching a given predicate.

Example:

Kotlin
fun main() {
    val list = listOf("Computer", "Kotlin", "Geek")
    println(list.first { it.startsWith("C") }) // First word starting with 'C'
    println(list.last { it.endsWith("k") })   // Last word ending with 'k'
}

Output: 

Computer
Geek


Both functions throw exceptions, if no elements found matching the predicate. To avoid exception, we use firstOrNull() and lastOrNull() and they will return null if no matching elements are found. 

Kotlin
fun main() {
    val list = listOf("Computer", "Kotlin", "Geek")
    println(list.firstOrNull { it.startsWith("Z") }) // no such element
    println(list.lastOrNull { it.endsWith("z") })    // no such element
}

Output: 

null
null


4. Random Element Retrieval

If we want to retrieve an arbitrary element from the list we can use random() function. 

random() :- It returns a random element from the collection.

Example:

Kotlin
fun main() {
    val list = listOf("Computer", "Kotlin", "Geek")
    println(list.random()) // Output can vary each time
}

Output: 

Computer 


5. Checking Existence of Elements

We can also check if an element exists in the collection.

  1. 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.
  2. 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.
  3. 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. 
  4. 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. 
  5. 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.  

Example:

Kotlin
fun main() {
    val list = listOf("Geeks", "Kotlin", "Portal")

    println("Java" in list)
    println("Kotlin" in list)
    println(list.containsAll(listOf("Kotlin", "Portal")))
    println(list.isEmpty())
    println(list.isNotEmpty())
}

Output:

false
true
true
false
true


 


Article Tags :

Similar Reads