Difference between revisions of "KOTLIN: Collection"
Onnowpurbo (talk | contribs) (Created page with "Sumber: https://www.tutorialspoint.com/kotlin/kotlin_collections.htm Collections are a common concept for most programming languages. A collection usually contains a number...") |
Onnowpurbo (talk | contribs) |
||
| Line 7: | Line 7: | ||
The Kotlin Standard Library provides a comprehensive set of tools for managing collections. The following collection types are relevant for Kotlin: | The Kotlin Standard Library provides a comprehensive set of tools for managing collections. The following collection types are relevant for Kotlin: | ||
| − | Kotlin List - List is an ordered collection with access to elements by indices. Elements can occur more than once in a list. | + | Kotlin List - List is an ordered collection with access to elements by indices. Elements can occur more than once in a list. |
| + | Kotlin Set - Set is a collection of unique elements which means a group of objects without repetitions. | ||
| + | Kotlin Map - Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value. | ||
| − | Kotlin | + | ==Kotlin Collection Types== |
| − | |||
| − | |||
| − | |||
Kotlin provides the following types of collection: | Kotlin provides the following types of collection: | ||
| − | Collection or Immutable Collection | + | Collection or Immutable Collection |
| + | Mutable Collection | ||
| − | + | ==Kotlin Immutable Collection== | |
| − | |||
Immutable Collection or simply calling a Collection interface provides read-only methods which means once a collection is created, we can not change it because there is no method available to change the object created. | Immutable Collection or simply calling a Collection interface provides read-only methods which means once a collection is created, we can not change it because there is no method available to change the object created. | ||
| − | Collection Types Methods of Immutable Collection | + | Collection Types Methods of Immutable Collection |
| − | List listOf() | + | List listOf() |
| − | listOf<T>() | + | listOf<T>() |
| − | Map mapOf() | + | Map mapOf() |
| − | Set setOf() | + | Set setOf() |
| − | Example | + | |
| − | fun main() { | + | ==Example== |
| − | + | ||
| − | + | fun main() { | |
| − | + | val numbers = listOf("one", "two", "three", "four") | |
| − | } | + | |
| + | println(numbers) | ||
| + | } | ||
| + | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
| − | [one, two, three, four] | + | [one, two, three, four] |
| − | Kotlin Mutable Collection | + | |
| + | ==Kotlin Mutable Collection== | ||
| + | |||
Mutable collections provides both read and write methods. | Mutable collections provides both read and write methods. | ||
| − | Collection Types Methods of Immutable Collection | + | Collection Types Methods of Immutable Collection |
| − | List ArrayList<T>() | + | List ArrayList<T>() |
| − | arrayListOf() | + | arrayListOf() |
| − | mutableListOf() | + | mutableListOf() |
| − | Map HashMap | + | Map HashMap |
| − | hashMapOf() | + | hashMapOf() |
| − | mutableMapOf() | + | mutableMapOf() |
| − | Set hashSetOf() | + | Set hashSetOf() |
| − | mutableSetOf() | + | mutableSetOf() |
| − | Example | + | |
| − | fun main() { | + | ===Example=== |
| − | + | ||
| − | + | fun main() { | |
| − | + | val numbers = mutableListOf("one", "two", "three", "four") | |
| − | + | ||
| − | + | numbers.add("five") | |
| − | } | + | |
| + | println(numbers) | ||
| + | } | ||
| + | |||
When you run the above Kotlin program, it will generate the following output: | When you run the above Kotlin program, it will generate the following output: | ||
| − | [one, two, three, four, five] | + | [one, two, three, four, five] |
| + | |||
Note that altering a mutable collection doesn't require it to be a var. | Note that altering a mutable collection doesn't require it to be a var. | ||
| − | Quiz Time (Interview & Exams Preparation) | + | |
| + | ==Quiz Time (Interview & Exams Preparation)== | ||
| + | |||
Q 1 - Which of the following is true about Kotlin Collections? | Q 1 - Which of the following is true about Kotlin Collections? | ||
Latest revision as of 10:02, 23 July 2022
Sumber: https://www.tutorialspoint.com/kotlin/kotlin_collections.htm
Collections are a common concept for most programming languages. A collection usually contains a number of objects of the same type and Objects in a collection are called elements or items.
The Kotlin Standard Library provides a comprehensive set of tools for managing collections. The following collection types are relevant for Kotlin:
Kotlin List - List is an ordered collection with access to elements by indices. Elements can occur more than once in a list. Kotlin Set - Set is a collection of unique elements which means a group of objects without repetitions. Kotlin Map - Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value.
Kotlin Collection Types
Kotlin provides the following types of collection:
Collection or Immutable Collection Mutable Collection
Kotlin Immutable Collection
Immutable Collection or simply calling a Collection interface provides read-only methods which means once a collection is created, we can not change it because there is no method available to change the object created.
Collection Types Methods of Immutable Collection List listOf() listOf<T>() Map mapOf() Set setOf()
Example
fun main() {
val numbers = listOf("one", "two", "three", "four")
println(numbers)
}
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four]
Kotlin Mutable Collection
Mutable collections provides both read and write methods.
Collection Types Methods of Immutable Collection List ArrayList<T>() arrayListOf() mutableListOf() Map HashMap hashMapOf() mutableMapOf() Set hashSetOf() mutableSetOf()
Example
fun main() {
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five")
println(numbers)
}
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four, five]
Note that altering a mutable collection doesn't require it to be a var.
Quiz Time (Interview & Exams Preparation)
Q 1 - Which of the following is true about Kotlin Collections?
A - Kotlin provides mutable and immutable collection
B - List, Set and Map are Kotlin Collections
C - Kotlin Map can store values in Key-Value pairs
D - All of the above
Q 2 - What will be the output of the following program:
fun main() {
val numbers = listOf("one", "two", "three", "four")
numbers = listOf("five")
} A - This will print 0
B - This will raise just a warning
C - Compilation will stop with error
D - None of the above
Q 2 - Which statement is not correct?
A - Kotlin List can have duplicate values
B - Kotlin Set can not have duplicate values
C - Immutable collection does not provide methods to change/update the collection.
D - Kotlin does not provide collection types: sets, lists, and maps