
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - single() Function
The Kotlin array single() function is used to check whether single element is available in an array. If an array is empty and more than one element is available, then it returns an error.
This function also takes a predicate and return single element matching the predicate. Otherwise; throws an exception if there is no or more than one matching element.
Exception
Following are the exceptions −
IllegalArgumentException − If an array contains more than one elements.
NoSuchElementException − If an array is empty.
Syntax
Following is the syntax of Kotlin array single() function −
fun <T> Array<out T>.single( predicate: (T) -> Boolean ): T
Parameters
This function accepts a predicate represents boolean value, true if conditon satisfied. Otherwise; false.
Return value
This function returns an element. Otherwise; an exception.
Example 1
The following is a basic example to demonstrate the use of single() function −
fun main(args: Array<String>) { val numbers = arrayOf<Any>(1) // use single val single = numbers.single() println(single) }
Output
The above code generate following output −
1
Example 2
Now, Let's see another example, we use single() function with predicate to returns a single element matching with predicate −
fun main(args: Array<String>) { val numbers = arrayOf(1, 2, 3, 4, 5) try { val single = numbers.single { it % 3 == 0 } println("Array has a single element: $single") } catch (e: NoSuchElementException) { println("Array is empty.") } catch (e: IllegalArgumentException) { println("Array contains more than one element that matches the predicate.") } }
Output
Following is the output −
Array has a single element: 3
Example 3
The below example checks whether in the string array contains exactly one elements having length 4 −
fun main(args: Array<String>) { val numbers = arrayOf<String>("tutorialspoint", "Hyderabad", "India") try { val single = numbers.single { it.length > 4 } println("Array has a single element: $single") } catch (e: NoSuchElementException) { println("Array is empty.") } catch (e: IllegalArgumentException) { println("Array contains more than one element that matches the predicate.") } }
Output
Following is the output −
Array contains more than one element that matches the predicate.