

We can use windowed() method to a list of element ranges by moving a sliding window of a given size over a collection of elements. Last chunk may not have the elements equal to the number of chunk size based on the total number of elements in the list. We can use chunked() method to create chunks of the given size from a list. We can use map() method to map all elements using the provided function.

We can use filter() method to filter out the elements matching with the given predicate. We can use filterNotNull() method to remove null elements from a Kotlin list. Val theList = listOf("one", "two", "three", "four", "five") We can obtain a sublist from a given list using slice() method which makes use of range of the elements indices. Val secondList = listOf("one", "five", "six") This operation will remove the common elements from the first list and will return the result. We can use - operator to subtract a list from another list. Val secondList = listOf("four", "five", "six") Val firstList = listOf("one", "two", "three") This will add second list into first list, even duplicate elements will also be added. We can use + operator to add two or more lists into a single list. Println("Element at 3rd position " + theList.get(2)) The get() method can be used to get the element at the specified index in the list. Println("Index of 'two' : " + theList.indexOf("two")) The indexOf() method returns the index of the first occurrence of the specified element in the list, or -1 if the specified element is not contained in the list. The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise. The contain() method can also be used to check the existence of an element in a list. The in operator can be used to check the existence of an element in a list. Println("Size of the list " + theList.size) Val theList = listOf("one", "two", null, "four", "five") We can use size property to get the total number of elements in a list: Note - here it works like this operator in Java. Lets study them one by one: Using toString() function There are various ways to loop through a Kotlin list. When you run the above Kotlin program, it will generate the following output: Val theMutableList = mutableListOf("one", "two", "three", "four") Val theList = listOf("one", "two", "three", "four") To prevent unwanted modifications, obtain read-only views of mutable lists by casting them to List. Creating Kotlin Listsįor list creation, use the standard library functions listOf() for read-only lists and mutableListOf() for mutable lists.

Kotlin mutable or immutable lists can have duplicate elements. The elements of list can be accessed using indices. A Kotlin list can be either mutable ( mutableListOf) or read-only ( listOf).

Kotlin list is an ordered collection of items.
