It’s time to continue our learning path in Kotlin. The subject covered in this new post is represented by Collections and data operations applied to them.
Collections are actually a set of classes and interfaces which provide high quality implementations of useful data structures and algorithms that help developers to reduce the programming effort and time.
In Kotlin there are different flavors of collections. Just like Java, all collection interfaces are originated from the Iterable interface. The main difference between Java and Kotlin is that in Kotlin we have mutable and immutable collections. An important thing to mention here is that immutable collections in Kotlin are simply read-only.
So in terms of collections these are the main ones:
- Pair — a tuple of two values and Triple — a tuple of three values.
- Array — indexed fixed-sized collection of objects and primitives.
- List — ordered collection of objects.
- Set — unordered collection of objects.
- Map — associative dictionary or map of keys and values.
Are very helpful if we want to quickly create two (Pair) or three objects (Triple) as a collection
data class Pair<out A, out B> : Serializable
data class Triple<out A, out B, out C> : Serializable
A block memory that keeps multiple values in a sequence; very helpful for low-level optimizations.
Both the immutableArray and mutableArray arrays are fixed in size, but the elements of each array are mutable and can be updated. The only difference is that immutableArray is declared with the val keyword, so this array cannot be reassigned.
Are data structures that hold a number of items in a sequence. Kotlin provides two different types of lists:
- immutable lists (does not have add function)
- mutable lists
interface List<out E> :[Collection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html)<E>
interface MutableList<E> : [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html)<E>, [MutableCollection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-collection/index.html)<E>
Are unordered collections of elements. Other than using setOf() / Set and mutableSetOf() / MutableSet we could also use hashSetOf() / java.util.HashSet
interface Set<out E> : [Collection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html)<E>
interface MutableSet<E> : [Set](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html)<E>, [MutableCollection](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-collection/index.html)<E>
Keeps a collection of key-value pairs.
interface Map<K, out V>
interface MutableMap<K, V> : [Map](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html)<K, V>
Check my previous articles about Kotlin:
Enjoy and feel free to leave a comment if something is not clear or if you have questions. And if you like it please share!
Thank you for reading! 🙌🙏😍✌
Follow me on:
Originally published at http://magdamiu.com on March 14, 2020.