List in Kotlin
List in Kotlin
A list is a generic ordered collection of elements. Kotlin has two types of lists, immutable
lists (cannot be modified) and mutable lists (can be modified).
Read-only lists are created with listOf() whose elements cannot be modified and mutable
lists created with mutableListOf() method where we alter or modify the elements of the list.
Kotlin program of list contains Integers –
Output:
Output:
1
6
The first index of number is 0
The last index of number is 6
The last index of the list is 8
Output:
1
10
List Iteration methods –
It is process of accessing the elements of list one by one.
There are several ways of doing this in Kotlin.
// method 3
names.forEachIndexed({i, j -> println("names[$i] = $j")})
// method 4
val it: ListIterator<String> = names.listIterator()
while (it.hasNext()) {
val i = it.next()
print("$i ")
}
println()
}
Output:
Gopal, Asad, Shubham, Aditya, Devarsh, Nikhil, Gagan,
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan
names[0] = Gopal
names[1] = Asad
names[2] = Shubham
names[3] = Aditya
names[4] = Devarsh
names[5] = Nikhil
names[6] = Gagan
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan
Explanation:
The for loop traverses the list. In each cycle, the variable ‘name’ points to the next
element in the list.
This method uses the size of the list. The until keyword creates a range of the list
indexes.
With the forEachIndexed() method, we loop over the list having index and value
available in each iteration.
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1, 0]
Explanation:
val asc = list.sorted()
if (res)
println("The list contains 0")
else
println("The list does not contain 0")
if (result)
println("The list contains 3 and -1")
else
println("The list does not contain 3 and -1")
}
Output:
Checks whether the list contains 0 or not and returns true or false (her true) that is
stored in res.
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
if("two" in theList){
println(true)
}else{
println(false)
}
}
When you run the above Kotlin program, it will generate the following output:
true
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
if(theList.contains("two")){
println(true)
}else{
println(false)
}
}
When you run the above Kotlin program, it will generate the following output:
true
The isEmpty() method returns true if the collection is empty (contains no elements),
false otherwise.
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
if(theList.isEmpty()){
println(true)
}else{
println(false)
}
}
When you run the above Kotlin program, it will generate the following output:
false
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.
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
When you run the above Kotlin program, it will generate the following output:
Index of 'two' : 1
The get() Method
The get() method can be used to get the element at the specified index in the list.
First element index will be zero.
Example
fun main() {
val theList = listOf("one", "two", "three", "four")
When you run the above Kotlin program, it will generate the following output:
List Addition
We can use + operator to add two or more lists into a single list. This will add second
list into first list, even duplicate elements will also be added.
Example
fun main() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("four", "five", "six")
val resultList = firstList + secondList
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four, five, six]
List Subtraction
We can use - operator to subtract a list from another list. This operation will remove
the common elements from the first list and will return the result.
Example
fun main() {
val firstList = listOf("one", "two", "three")
val secondList = listOf("one", "five", "six")
val resultList = firstList - secondList
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
[two, three]
Slicing a List
We can obtain a sublist from a given list using slice() method which makes use of
range of the elements indices.
Example
fun main() {
val theList = listOf("one", "two", "three", "four", "five")
val resultList = theList.slice( 2..4)
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can use filterNotNull() method to remove null elements from a Kotlin list.
fun main() {
val theList = listOf("one", "two", null, "four", "five")
val resultList = theList.filterNotNull()
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
Filtering Elements
We can use filter() method to filter out the elements matching with the given
predicate.
fun main() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.filter{ it > 30}
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can use drop() method to drop first N elements from the list.
fun main() {
val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultList = theList.drop(3)
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can use groupBy() method to group the elements matching with the given
predicate.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.groupBy{ it % 3}
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can use map() method to map all elements using the provided function:.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.map{ it / 3 }
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can use chunked() method to create chunks of the given size from a list. Last
chunk may not have the elements equal to the number of chunk size based on the
total number of elements in the list.
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.chunked(3)
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3)
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [12, 30, 31], [30, 31, 40], [31, 40, 9], [40, 9, -3], [9, -3, 0]]
By default, the sliding window moves one step further each time but we can change
that by passing a custom step value:
fun main() {
val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultList = theList.windowed(3, 3)
println(resultList)
}
When you run the above Kotlin program, it will generate the following output:
We can create mutable list using mutableListOf(), later we can use add() to add more
elements in the same list, and we can use remove() method to remove the elements
from the list.
fun main() {
val theList = mutableListOf (10, 20, 30)
theList.add(40)
theList.add(50)
println(theList)
theList.remove(10)
theList.remove(30)
println(theList)
}
When you run the above Kotlin program, it will generate the following output:
List1: []
List1: [noodles]
List1: []
Empty? true