-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path044_Collection.kt
More file actions
27 lines (24 loc) · 1.16 KB
/
044_Collection.kt
File metadata and controls
27 lines (24 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
Types of Collections
In Kotlin collections are categories into two forms.
1. Immutable Collection
2. Mutable Collection
Immutable means that it supports only read-only functionalities and can not be modified its elements. Immutable Collections and their corresponding methods are:
1. List – listOf() and listOf<T>()
2. Set – setOf()
3. Map – mapOf()
Mutable Collection supports both read and write functionalities. Mutable collections and their corresponding methods are:
1. List – mutableListOf(),arrayListOf() and ArrayList
2. Set – mutableSetOf(), hashSetOf()
3. Map – mutableMapOf(), hashMapOf() and HashMap
*/
fun main() {
val numbers: MutableList<Int> = mutableListOf(1, 2, 3) //mutable List
val readOnlyView: List<Int> = numbers // immutable list
println("my mutable list--"+numbers) // prints "[1, 2, 3]"
numbers.add(4)
println("my mutable list after addition --"+numbers) // prints "[1, 2, 3, 4]"
println(readOnlyView)
// readOnlyView.clear() // does not compile
// gives error because we try to clear immutable list of collection
}