Kotlin Tutorials

Kotlin Set singleOrNull()
Syntax & Examples

Set.singleOrNull() extension function

The singleOrNull() extension function in Kotlin returns the single element in the collection, or null if the collection is empty or has more than one element. It can also return the single element matching the given predicate, or null if no element matches the predicate or more than one element matches the predicate.


Syntax of Set.singleOrNull()

There are 2 variations for the syntax of Set.singleOrNull() extension function. They are:

1.
fun <T> Set<T>.singleOrNull(): T?

This extension function returns single element, or null if the collection is empty or has more than one element.

Returns value of type T?.

2.
fun <T> Set<T>.singleOrNull(predicate: (T) -> Boolean): T?

Parameters

ParameterOptional/RequiredDescription
predicateoptionalA function that takes an element and returns a Boolean indicating whether the element matches the predicate.

This extension function returns the single element matching the given predicate, or null if element was not found or more than one element was found.

Returns value of type T?.



✐ Examples

1 Getting the single element in a set of integers or null

Using singleOrNull() to get the single element in a set of integers, or null if the set does not contain exactly one element.

For example,

  1. Create a set of integers with a single element.
  2. Use singleOrNull() to get the single element.
  3. Print the resulting element.

Kotlin Program

fun main() {
    val numbers = setOf(42)
    val singleNumber = numbers.singleOrNull()
    println(singleNumber)
}

Output

42

2 Getting the single element matching a predicate in a set of strings or null

Using singleOrNull() to get the single element matching a predicate in a set of strings, or null if there is no matching element or more than one matching element.

For example,

  1. Create a set of strings.
  2. Use singleOrNull() with a predicate to get the single matching element.
  3. Print the resulting element.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val singleString = strings.singleOrNull { it.length == 3 }
    println(singleString)
}

Output

two

3 Returning null when the set has more than one element

Using singleOrNull() to return null when the set has more than one element.

For example,

  1. Create a set of integers with multiple elements.
  2. Use singleOrNull() to try to get the single element, expecting null to be returned.
  3. Print the result.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    val singleNumber = numbers.singleOrNull()
    println(singleNumber)
}

Output

null

4 Returning null when no element matches the predicate

Using singleOrNull() to return null when no element matches the predicate.

For example,

  1. Create a set of strings.
  2. Use singleOrNull() with a predicate that matches no element, expecting null to be returned.
  3. Print the result.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    val singleString = strings.singleOrNull { it.length == 5 }
    println(singleString)
}

Output

null

Summary

In this Kotlin tutorial, we learned about singleOrNull() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.