Skip to main content
almarefa.net

Back to all posts

How to Create List/Set Of Functions In Kotlin?

Published on
5 min read
How to Create List/Set Of Functions In Kotlin? image

Best Kotlin Programming Guides to Buy in October 2025

1 Kotlin in Action, Second Edition

Kotlin in Action, Second Edition

BUY & SAVE
$45.98 $59.99
Save 23%
Kotlin in Action, Second Edition
2 Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)

BUY & SAVE
$29.95 $32.95
Save 9%
Kotlin In-Depth: A Guide to a Multipurpose Programming Language for Server-Side, Front-End, Android, and Multiplatform Mobile (English Edition)
3 Kotlin: An Illustrated Guide

Kotlin: An Illustrated Guide

BUY & SAVE
$49.62
Kotlin: An Illustrated Guide
4 Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines

BUY & SAVE
$48.00 $65.99
Save 27%
Programming Android with Kotlin: Achieving Structured Concurrency with Coroutines
5 Atomic Kotlin

Atomic Kotlin

BUY & SAVE
$44.91 $49.00
Save 8%
Atomic Kotlin
6 Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices

BUY & SAVE
$30.53 $44.99
Save 32%
Kotlin Design Patterns and Best Practices: Elevate your Kotlin skills with classical and modern design patterns, coroutines, and microservices
7 Functional Programming in Kotlin

Functional Programming in Kotlin

BUY & SAVE
$46.16 $49.99
Save 8%
Functional Programming in Kotlin
8 Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer

BUY & SAVE
$36.20 $59.99
Save 40%
Kotlin from Scratch: A Project-Based Introduction for the Intrepid Programmer
+
ONE MORE?

In Kotlin, you can create a list or set of functions just like you would with any other data type. Here's how you can do it:

  1. Define a function: To create a list or set of functions, you first need to define the individual functions. For example, let's consider two functions - multiplyByTwo and divideByTwo:

fun multiplyByTwo(number: Int): Int { return number * 2 }

fun divideByTwo(number: Int): Int { return number / 2 }

  1. Create a list of functions: To create a list of functions, you can use the listOf function and provide the function names as parameters:

val functionList = listOf(::multiplyByTwo, ::divideByTwo)

Now, functionList contains the references to both multiplyByTwo and divideByTwo functions.

  1. Create a set of functions: Similarly, to create a set of functions, you can use the setOf function:

val functionSet = setOf(::multiplyByTwo, ::divideByTwo)

Now, functionSet contains a set of references to both functions.

With the list or set of functions created, you can iterate over them, invoke them dynamically using reflection, or perform other operations based on your requirements.

How to invoke all functions in a list in Kotlin?

To invoke all functions in a list in Kotlin, you can use the forEach function to iterate over each element of the list and invoke the functions.

Here's an example:

fun firstFunction() { println("First function") }

fun secondFunction() { println("Second function") }

fun thirdFunction() { println("Third function") }

fun main() { // Create a list of functions val functions = listOf(::firstFunction, ::secondFunction, ::thirdFunction)

// Invoke all functions in the list
functions.forEach { it.invoke() }

}

In this example, we define three functions (firstFunction, secondFunction, thirdFunction) and store them in a list called functions using the :: operator to create function references.

Then, we use the forEach function to iterate over each element in the functions list and invoke the function using the invoke method. The invoke method is used to call the function.

When you run the main function, it will invoke all the functions in the list, and the output will be:

First function Second function Third function

How to pass a list of functions as a parameter in Kotlin?

To pass a list of functions as a parameter in Kotlin, you can use the List interface along with function types.

Here is an example of how you can do it:

// Define a function that takes a list of functions as a parameter fun processFunctions(functions: List<() -> Unit>) { // Iterate over the list and execute each function for (function in functions) { function.invoke() } }

// Define some functions to be passed as parameters fun function1() { println("Function 1") }

fun function2() { println("Function 2") }

fun function3() { println("Function 3") }

// Create a list of functions val functions = listOf(::function1, ::function2, ::function3)

// Call the function and pass the list of functions processFunctions(functions)

In the example above, the processFunctions function takes a List<() -> Unit> as a parameter, which means it accepts a list of functions that take no arguments and return Unit. The function then iterates over the list and invokes each function using the invoke() method.

You can define your own functions and add them to the list. In this example, the functions function1, function2, and function3 are defined and added to the functions list.

How to declare and initialize a list in Kotlin?

To declare and initialize a list in Kotlin, you can follow these steps:

  1. Immutable List: If you want to declare an immutable list, which means you cannot modify its elements after initialization, use the listOf() function:

val myImmutableList = listOf("apple", "banana", "orange")

  1. Mutable List: If you want to declare a mutable list, where you can add, remove, or modify its elements, use the mutableListOf() function:

val myMutableList = mutableListOf("apple", "banana", "orange")

  1. Empty List: If you want to declare an empty list and add elements to it later, you can use the emptyList() or mutableListOf() functions without passing any initial elements:

val emptyImmutableList = emptyList() val emptyMutableList = mutableListOf()

In the above examples, the elements of the list are strings ("apple", "banana", "orange"), but you can replace them with elements of any other data type.

Note: The val keyword is used to declare a read-only variable (immutable), while the var keyword is used to declare a mutable variable.

How to declare and initialize a list of functions in Kotlin?

To declare and initialize a list of functions in Kotlin, you can follow these steps:

  1. Declare a variable of type List with the function signature as the element type.

val functionList: List<() -> Unit>

  1. Initialize the list either by using the listOf or mutableListOf functions.

val functionList: List<() -> Unit> = listOf(::function1, ::function2, ::function3)

  1. You can also initialize an empty list and then add functions to it using the add or plusAssign operators.

val functionList: MutableList<() -> Unit> = mutableListOf()

functionList.add(::function1) functionList.add(::function2) functionList += ::function3

  1. Invoke the functions in the list by iterating over the list and calling each function.

for (function in functionList) { function.invoke() }

// Or, you can use the forEach function of the list. functionList.forEach { it.invoke() }

Note that () -> Unit represents the function signature that takes no arguments and returns no value. You can replace it with the appropriate signature based on your requirements.