Kotlin Set single()
Syntax & Examples


Set.single() extension function

The single() extension function in Kotlin returns the single element in the collection, or throws an exception if the collection is empty or has more than one element. It can also return the single element matching the given predicate, or throw an exception if there is no matching element or more than one matching element.


Syntax of Set.single()

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

1.
fun <T> Set<T>.single(): T

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

Returns value of type T.

2.
fun <T> Set<T>.single(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 throws exception if there is no or more than one matching element.

Returns value of type T.



✐ Examples

1 Getting the single element in a set of integers

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

For example,

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

Kotlin Program

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

Output

42

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

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

For example,

  1. Create a set of strings.
  2. Use single() 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.single { it.length == 3 }
    println(singleString)
}

Output

two

3 Handling exception when there are multiple elements in the set

Using single() to demonstrate the exception thrown when the set has more than one element.

For example,

  1. Create a set of integers with multiple elements.
  2. Use single() to try to get the single element, expecting an exception to be thrown.
  3. Handle the exception and print an appropriate message.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    try {
        val singleNumber = numbers.single()
        println(singleNumber)
    } catch (e: IllegalArgumentException) {
        println("Exception: ${e.message}")
    }
}

Output

Exception: Collection contains more than one element.

4 Handling exception when no element matches the predicate

Using single() to demonstrate the exception thrown when no element matches the predicate.

For example,

  1. Create a set of strings.
  2. Use single() with a predicate that matches no element, expecting an exception to be thrown.
  3. Handle the exception and print an appropriate message.

Kotlin Program

fun main() {
    val strings = setOf("one", "two", "three")
    try {
        val singleString = strings.single { it.length == 5 }
        println(singleString)
    } catch (e: IllegalArgumentException) {
        println("Exception: ${e.message}")
    }
}

Output

Exception: Collection contains no element matching the predicate.

Summary

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