Kotlin Collections With Examples #1

Vefa Can Beytorun
Paycell Tech Team
Published in
4 min readJan 8, 2024

In the realm of modern programming languages, Kotlin stands out for its concise syntax, interoperability, and expressive features. One of its key strengths lies in its robust collection framework, offering a versatile set of tools to handle data manipulation efficiently. Whether you’re a seasoned developer or just starting with Kotlin, understanding its collection framework is essential for unleashing the full potential of the language.

Lets explore the intricacies of Kotlin collections, discovering how they simplify code, enhance readability, and empower developers to tackle a wide array of tasks with elegance and efficiency.

Collections are divided into three as given below:

Let’s examine these three main topics with examples. (In this section of our article, we will explore collection operators that can be used in all three main headings and specific operators for the list heading.)

1. Collections Operations with List and List’s Specific Operations

In Kotlin, a List is a fundamental collection type that represents an ordered collection of elements. It is part of the Kotlin standard library and provides various methods for accessing, modifying, and iterating over its elements.

size

Returns the number of elements in the list. Here’s an example:

getOrNull and getOrElse

getOrNull function calls the data in the selected index and returns null if there is no data. getOrElse returns the value we wrote into {} if the value in the selected index does not exist. Here’s an example:

filter

filter is used to select elements from a list based on a specified condition. Here are two examples about to filter:

forEach

It is a function used to perform a specific operation on each element in the list. forEach is similar with for but forEach function can help make code more readable because it can express the operation in a single line and hide looping operations over the collection. Here’s an example:

Also you can write this code with for:

contains

This function is useful for searching within a list and querying whether a particular element is found. Here’s an example:

count

It is used to return the number of elements in a list that satisfy a certain condition. Here’s an example:

isEmpty

isEmpty is checks if a list is empty and returns a specific value (an alternative value) if the list is empty. Here’s an example:

subList

In Kotlin, you can use the subList function to obtain a sublist from an existing list. The subList function takes two parameters: the starting index and the ending index of the sublist. Here’s an example:

Linear Search

indexOf shows the index of the value we wrote into. lastIndexOf shows the last index of the value we wrote in the list. Here’s an example:

Sorting

In Kotlin sorting algorithm processes numbers from smallest to largest, while for letters it goes in dictionary order from the first letter of the alphabet to the last letter. Here’s an example:

Add

You can add any data you want to specific indexes of the list. Here’s an example:

Update

Also you can update specific indexes in list. Here’s an example:

fill() simply replaces all the collection elements with the specified value. Here’s an example:

Remove

In Kotlin, you can use removeAt is a function that is used to remove an element from a specific index in a mutable list. Here’s an example:

Reverse and Shuffle

You can reverse and shuffle the list with reverse() and shuffle() keywords. Here’s an example:

binarySearch

Searches an ordered list based on a specific attribute. For example, you can search a list of objects by a field that represents a particular property. Here’s an example:

elementAtOrElse

Retrieves the element at the specified index or returns a provided default value if the index is not valid. Here’s an example:

zipWithNext

Pairs an element with each subsequent element. A pair is created for each element except the first and last element of the list. Here’s an example:

chunked

chunked is used to divide the elements of a list into groups of a specified size. Here’s an example:

distinctBy

Returns a new list containing unique items based on a specified attribute. Here’s an example:

partition

Splits the list into two groups based on a specified condition. Here’s an example:

withIndex

Creates a pair containing list items and their indexes. Here’s an example:

flatMap

flatMap is applies a lambda expression for each element and creates a new list containing the conversion result of each element. It then merges all of these lists into one flattened list. Here’s an example:

all

It is used to check whether all elements in the list satisfy a certain condition and it returns true or false. Here’s an example:

any

It is used to check whether at least one element in the list satisfies a certain condition and it returns true or false. Here’s an example:

toSet

Converts a list into a set data structure. Here’s an example:

Conclusion

We explored the fundamental collection structures and operators in the Kotlin programming language. The functions we learned about on the List collection simplify data manipulation and enhance code readability. Kotlin collections provide developers with fast and efficient data management, easing basic operations. Documentation and the standard library offer easy access to these features, providing developers with a deeper understanding.

HAPPY NEW YEAR

References

--

--