- Kotlin - Exception Handling
- Kotlin - Destructuring Declarations
- Kotlin - Delegation
- Kotlin - Generics
- Kotlin - Sealed Class
- Kotlin - Data Classes
- Kotlin - Extension
- Kotlin - Visibility Control
- Kotlin - Interface
- Kotlin - Abstract Classes
- Kotlin - Inheritance
- Kotlin - Constructors
- Kotlin - Class and Objects
- Kotlin - Maps
- Kotlin - Sets
- Kotlin - Lists
- Kotlin - Collections
- Kotlin - Break and Continue
- Kotlin - While Loop
- Kotlin - For Loop
- Kotlin - When Expression
- Kotlin - if...Else Expression
- Kotlin - Control Flow
- Kotlin - Functions
- Kotlin - Ranges
- Kotlin - Arrays
- Kotlin - Strings
- Kotlin - Booleans
- Kotlin - Operators
- Kotlin - Data Types
- Kotlin - Variables
- Kotlin - Keywords
- Kotlin - Comments
- Kotlin - Basic Syntax
- Kotlin - Architecture
- Kotlin - Environment Setup
- Kotlin - Overview
- Kotlin - Home
Kotlin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Kotpn - Collections
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 Kotpn Standard Library provides a comprehensive set of tools for managing collections. The following collection types are relevant for Kotpn:
Kotpn List - List is an ordered collection with access to elements by indices. Elements can occur more than once in a pst.
Kotpn Set - Set is a collection of unique elements which means a group of objects without repetitions.
Kotpn Map - Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value.
Kotpn Collection Types
Kotpn provides the following types of collection:
Collection or Immutable Collection
Mutable Collection
Kotpn Immutable Collection
Immutable Collection or simply calpng 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 | pstOf() pstOf<T>() |
Map | mapOf() |
Set | setOf() |
Example
fun main() { val numbers = pstOf("one", "two", "three", "four") println(numbers) }
When you run the above Kotpn program, it will generate the following output:
[one, two, three, four]
Kotpn 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 Kotpn 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 Kotpn Collections?
Answer : D
Explanation
All the given statements are true about Kotpn Collections
Q 2 - What will be the output of the following program:
fun main() { val numbers = pstOf("one", "two", "three", "four") numbers = pstOf("five") }
Answer : C
Explanation
This will stop with error: val cannot be reassigned.
Q 2 - Which statement is not correct?
Answer : D
Explanation
Kotpn provides collection types: sets, psts, and maps
Advertisements