

Kotlin distinguishes between immutable and mutable maps. Map keys are unique and the map holds only one value for each key. Map holds the data in the form of pairs which consists of a key and a value. Val mutableList = mutableListOf(1, 3, 9, 16)Ĭoncluding this introductory tutorial to Mutable List in Kotlin, we have covered only basic topics like creating, adding an element and removing an element. Kotlin map is a collection that contains pairs of objects. In the following program, we will create a mutable list of integers, and remove the element 9 from the mutable list using remove(element) function. The syntax to use remove() function with element is MutableList.remove(element) The first occurrence, if any, of this specified element will be removed from this mutable list. To remove an element from a Mutable List, call remove() function on this mutable list and pass the element. In the following program, we will create a mutable list of integers, and add elements to it using add(element) and add(index, element) functions. The syntax to use add() function with specific index and element is MutableList.add(index, element) The syntax to use add() function with element is MutableList.add(element) We can also specify the index, at which the element has to be inserted. The new element will be added at the end of the list.

To add an element to a Mutable List, call add() function on this mutable list and pass the element. In the following program, we will create a mutable list of integers. The syntax of mutableListOf() function is fun mutableListOf(vararg elements: T): MutableList To create a mutable list in Kotlin, call mutableListOf() function and pass the elements to it.
