Industrial Training




Kotlin List Interface


Kotlin List is an interface and generic collection of elements. The List interface inherits form Collection< T> class. It is immutable and its methods supports only read functionalities.
To use the List interface we need to use its function called listOf(), listOf< E>().
The elements of list follow the sequence of insertion order and contains index number same as array.


List Interface Declaration


public interface List< out E> : Collection< E> (source)  
public interface List< out E> : Collection< E> (source)  

Function of Kotlin List Interface


There are several functions are available in the List interface. Some functions of List interface are mention below.


Functions Descriptions
abstract fun contains(element: E): Boolean

It checks specified element is contained in this collection.

abstract fun containsAll(elements: Collection): Boolean

It checks all elements specified are contained in this collection.

abstract operator fun get(index: Int): E

It returns the element at given index from the list.

abstract fun indexOf(element: E): Int

Returns the index of first occurrence of specified element in the list, or -1 if specified element is not present in list.

abstract fun isEmpty(): Boolean

It returns the true if list is empty, otherwise false.

abstract fun iterator(): Iterator< E>

It returns an iterator over the elements of this list.

abstract fun lastIndexOf(element: E): Int

It returns the index of last occurrence of specified element in the list, or return -1 if specified element is not present in list.

abstract fun listIterator(): ListIterator

It returns a list iterator over the elements in proper sequence in current list.

abstract fun listIterator(index: Int): ListIterator

It returns a list iterator over the elements in proper sequence in current list, starting at specified index.

abstract fun subList(fromIndex: Int, toIndex: Int): List

It returns a part of list between fromIndex (inclusive) to toIndex (exclusive).


Kotlin List Example 1


Let's see an example of list using listOf() function.


fun main(args: Array< String>){  
    var list = listOf("Ajay","Vijay","Prakash")//read only, fix-size  
    for(element in list){  
        println(element)  
    }  
fun main(args: Array< String>){  
    var list = listOf("Ajay","Vijay","Prakash")//read only, fix-size  
    for(element in list){  
        println(element)  
    }  			  

Output:
Ajay
Vijay
Prakash

Kotlin List Example 2


In the listOf() function we can pass the different types of data at the same time. List can also traverse the list using index range.


fun main(args: Array< String>){  
    var list = listOf(1,2,3,"Ajay","Vijay","Prakash")//read only, fix-size  
    for(element in list){  
        println(element)  
    }  
    println()  
    for(index in 0..list.size-1){  
        println(list[index])  
    }  
}  
fun main(args: Array< String>){  
    var list = listOf(1,2,3,"Ajay","Vijay","Prakash")//read only, fix-size  
    for(element in list){  
        println(element)  
    }  
    println()  
    for(index in 0..list.size-1){  
        println(list[index])  
    }  
} 

Output:
1
2
3
Ajay
Vijay
Prakash

1
2
3
Ajay
Vijay
Prakash

Kotlin List Example 3


For more specific we can provide the generic types of list such as listOf< Int>(), listOf< String>(), listOf< Any>() Let's see the example.


fun main(args: Array< String>){  
    var intList: List< Int> = listOf< Int>(1,2,3)  
    var stringList: List< String> = listOf< String>("Ajay","Vijay","Prakash")  
    var anyList: List< Any> = listOf< Any>(1,2,3,"Ajay","Vijay","Prakash")  
    println("print int list")  
    for(element in intList){  
        println(element)  
    }  
    println()  
    println("print string list")  
    for(element in stringList){  
        println(element)  
    }  
    println()  
    println("print any list")  
    for(element in anyList){  
        println(element)  
    }  
}  
fun main(args: Array< String>){  
    var intList: List< Int> = listOf< Int>(1,2,3)  
    var stringList: List< String> = listOf< String>("Ajay","Vijay","Prakash")  
    var anyList: List< Any> = listOf< Any>(1,2,3,"Ajay","Vijay","Prakash")  
    println("print int list")  
    for(element in intList){  
        println(element)  
    }  
    println()  
    println("print string list")  
    for(element in stringList){  
        println(element)  
    }  
    println()  
    println("print any list")  
    for(element in anyList){  
        println(element)  
    }  
}

Output:
print int list
1
2
3

print string list
Ajay
Vijay
Prakash

print any list
1
2
3
Ajay
Vijay
Prakash

Kotlin List Example 4


Let's see the use of different function of Kotlin list interface using listOf< T>() function.


fun main(args: Array< String>){  
    var stringList: List< String> = listOf< String>("Ajay","Vijay","Prakash","Vijay","Rohan")  
    var list: List< String> = listOf< String>("Ajay","Vijay","Prakash")  
    for(element in stringList){  
        print(element+" ")  
    }  
    println()  
    println(stringList.get(0))  
    println(stringList.indexOf("Vijay"))  
    println(stringList.lastIndexOf("Vijay"))  
    println(stringList.size)  
    println(stringList.contains("Prakash"))  
    println(stringList.containsAll(list))  
    println(stringList.subList(2,4))  
    println(stringList.isEmpty())  
    println(stringList.drop(1))  
    println(stringList.dropLast(2))  
}  
fun main(args: Array< String>){  
    var stringList: List< String> = listOf< String>("Ajay","Vijay","Prakash","Vijay","Rohan")  
    var list: List< String> = listOf< String>("Ajay","Vijay","Prakash")  
    for(element in stringList){  
        print(element+" ")  
    }  
    println()  
    println(stringList.get(0))  
    println(stringList.indexOf("Vijay"))  
    println(stringList.lastIndexOf("Vijay"))  
    println(stringList.size)  
    println(stringList.contains("Prakash"))  
    println(stringList.containsAll(list))  
    println(stringList.subList(2,4))  
    println(stringList.isEmpty())  
    println(stringList.drop(1))  
    println(stringList.dropLast(2))  
}

Output:
Ajay Vijay Prakash Vijay Rohan 
Ajay
1
3
5
true
true
[Prakash, Vijay]
false
[Vijay, Prakash, Vijay, Rohan]
[Ajay, Vijay, Prakash]

The limitation of List interface is that it is immutable. It cannot add more elements in list after its declaration. To solve this limitation Collection framework provide mutable list.



Hi I am Pluto.