Ultimate Kotlin List Guide: A Step-by-Step Tutorial for Beginners

A list in Kotlin is an ordered collection of data(elements) with an unlimited list size. The size of the list can be increased based on elements. We have all usages List in Java as a beginner. A list in Kotlin is very useful because almost every data is processed and stored using a List. If you are a pro developer then you know about the benefits of using List and if you are a beginner then you are about to know using this tutorial. Using a list can significantly enhance your programming technique. In today’s tutorial, we will be going to learn about List initialization, inserting elements in the list, manipulating list items, and much more.

Ultimate Kotlin List Guide: A Step-by-Step Tutorial for Beginners

Lists are divided into 2 parts in Kotlin:

  1. Immutable List: Immutable lists are a type of list whose elements cannot be modified. This type of list has fast-ready operations and well optimized for mult-threaded operations.
  2. Mutable List: In a mutable list we can perform add, update, and delete CURD operations. It is likely slower than an immutable list.

Ultimate Kotlin List Guide: A Step-by-Step Tutorial for Beginners

  1. Integer List: In a mutable integer list, we can store integer-type values only.
  2. String List – Mutable
  3. Float List – Mutable
  4. Double Mutable List
  5. Boolean Mutable List
  6. Character Mutable list
  7. Custom Model Mutable List: This list is mainly what we will be using in our Kotlin apps where we want to store different types of custom object-related data into our List. In this type of List first, we will create a Custom Model class in Kotlin as per that class we will store our data in List.
  8. Nullable Mutable List: In a nullable list, we can store NULL values also but we have to pass ? (Question mark) Safe call operator. The? allow us to store a NULL value and make our variable type nullable.

In the above example, we have covered most of the mutable list types we will use in our Kotlin program.

The most commonly used List Methods in Kotlin

In my examples, I am using the custom model class list. As a developer, we all know we use a List to store custom types of data which is associated with Model classes. For this example, I am creating a model class Person which has 2 member variables name and age. In Kotlin we do not have to define a constructor, getter & setter in the data class.

  1. add(): The add method in List in Kotlin adds an element to List.
  2. remove(): The remove method in List removes the first occurrence of the given element.
  3. contains(): Check whether a List contains a specific element if yes then return True otherwise return False.
  4. get(): The get function in List returns us the item present on a specific Index position.
  5. size(): The size function returns the total length of the List.
  6. clear(): Delete all elements from the list.
  7. sort(): To sort the list as per key.
  8. filter(): The list filter function returns us a new List containing all the elements that match the given condition or predicate.
  9. map(): The map function transforms the list elements according to the given function.
  10. forEach(): The for each is to print all the elements of the List one by one.
  11. joinToString(): It concatenates the elements of the List into a single string with a given Char or String.
  12. find(): The find List function in Kotlin returns us the first condition matching element. If not found then return Null.
  13. groupBy(): the groupBy method in the list makes groups of the element by a specified key.
  14. any(): Checks if the element in the list matches the given predicate condition.

    I have tried my best to explain the most useful list method available in Kotlin. But there are a lot more also available in the official Kotlin documentation. You can read all of them from HERE.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *