VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/retrieve-single-elements-in-kotlin/

⇱ Retrieve Single Elements In Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Retrieve Single Elements In Kotlin

Last Updated : 15 Jun, 2025

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:

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:

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:

Output:

GeeksforGeeks
Portal


3. Retrieving by Condition (Predicate) -

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

Example:

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. 

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:

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:

Output:

false
true
true
false
true


Comment
Article Tags:

Explore