Kotlin List toBooleanArray()
Syntax & Examples


Syntax of List.toBooleanArray()

The syntax of List.toBooleanArray() extension function is:

fun Collection<Boolean>.toBooleanArray(): BooleanArray

This toBooleanArray() extension function of List returns an array of Boolean containing all of the elements of this collection.



✐ Example

1 Example

In this example,

  • We create a list named list containing the Boolean values true, false, true, false.
  • We call the toBooleanArray() function on list, which converts the list to a Boolean array.
  • The resulting Boolean array is stored in result.
  • Finally, we print the content of result to standard output using the contentToString() function.

Kotlin Program

fun main(args: Array<String>) {
    val list = listOf(true, false, true, false);
    val result = list.toBooleanArray();
    println(result.contentToString());
}

Output

[true, false, true, false]

Summary

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