Working with Kotlin Collections, Learn the Basics, Kotlin Programming Language

22 Jul 2023 Balmiki Mandal 0 Kotlin

Working with Kotlin Collections

Kotlin provides a well-structured way for dealing with collections. The language provides several classes and functions which handle collections effectively. This article covers the Kotlin collection concepts like lists, maps, sets, and ranges and explain how they can be used in practice.

Lists

Kotlin's List is an ordered collection that allows duplicate elements. In other words, a List can contain multiple instances of a single item. Elements in a List are indexed from zero. There are two types of lists: mutable and immutable. Mutable lists allow elements to be added, removed, and modified. On the other hand, immutable lists can't be changed once they have been created.

To create an empty list, you can use the listOf() function. You can also pass a list of elements to the mutableListOf() or arrayListOf().

// Create an empty list
val emptyList = listOf<String>()

// Create a mutable list of strings
val myList = mutableListOf("One", "Two", "Three")

// Create an array list of integers
val myArrayList = arrayListOf(1, 2, 3)

Maps

Kotlin's Map is an unordered collection of key-value pairs. It's an index-free collection where each element is associated with a key. Maps are useful when you need to store data with a specific key.

You can create an empty map using the mapOf() function. You can also pass a list of key-value pairs to the mutableMapOf() or hashMapOf() functions.

// Create an empty map
val emptyMap = mapOf<String, Int>()

// Create a mutable map of strings
val myMap = mutableMapOf("One" to 1, "Two" to 2, "Three" to 3)

// Create a hash mpa of integers
val myHashMap = hashMapOf(1 to "One", 2 to "Two", 3 to "Three")

Sets

Kotlin's Set is an unordered collection of elements. It doesn't allow duplicate elements. However, it does allow null values. Sets are great for storing unique values and checking whether an element exists in the set or not.

You can create an empty set using the setOf() function. You can also pass a list of elements to the mutableSetOf() or hashSetOf() functions.

// Create an empty set
val emptySet = setOf<String>()

// Create a mutable set of strings
val mySet = mutableSetOf("One", "Two", "Three")

// Create a hash set of integers
val myHashSet = hashSetOf(1, 2, 3)

Ranges

Kotlin's Range is a collection of consecutive numbers or characters. It's useful for performing operations on sequential collections, such as iterating over a range of elements or calculating the sum of a range of numbers.

You can create a range using the rangeTo() function. You can also use the until() and downTo() functions to create a range with an exclusive end point.

// Create a range of numbers
val myRange = (1..10)

// Create a range of characters
val myCharRange = ('a'..'z')

// Create a range with an exclusive end point
val myUntilRange = (1 until 10)
val myDownToRange = (10 downTo 1)

Conclusion

Kotlin provides a robust way of dealing with collections by providing several classes and functions. Lists, maps, sets, and ranges are the main types of collections supported by Kotlin. Understanding their differences and knowing how to use them is essential for working effectively with collections in Kotlin.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.