
Kotlin's collections give us the ability to achieve a lot with just a little code-unlike in Java, which seems to need a lot of code to achieve a little! Kotlin has two variants of collections: mutable and immutable. (If you want a refresher on arrays in Kotlin, kindly visit the first tutorial in this series.) In this section, we'll learn about the List, Set, and Map collections in Kotlin. To really understand the collections API in Kotlin, you'll need to be familiar with these basic classes and interfaces in Java. You can't see the implementation source code in Kotlin because the collections are actually implemented by the standard Java Collections such as ArrayList, Maps, HashMap, Sets, HashSet, List, and so on. You should note that these interfaces are linked to their implementation at compile time. (We'll discuss interfaces in Kotlin in a future post.) Kotlin provides its collections API as a standard library built on top of the Java Collections API. We can retrieve, store, or organize the objects in a collection. To define a range for the primitive type we use rangeTo() function.Collections are used to store groups of related objects in memory. There are built-in implementations of ranges for the integral primitive types, like Int, Long and Char. It stores handle to first and last elements of the range and also provides contains(value: T): Booleanand isEmpty(): Boolean functions that check two conditions: if the given value belongs to the range and if the range is empty. An interface from kotlin.ranges package called ClosedRange is a base declaration that models the idea of the range. The idea of a range itself can be seen as an abstract data type that models a closed scope of values or a set of objects that can be iterated through, in a concise way.

Let’s get started 🚀! Iterating through primitive types Next, we are going to implement a custom progression for the LocalDate class. First, we are going to discover built-in range implementations for the integral types like Char, Int or Long.

In this post, we are going to explore how to use range expressions in action.

In order to make the syntax for loop iteration and control flow statements safe and natural to read, Kotlin standard library provides a concept of range.
