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. let’s see how list interface declaration:

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

Function of List Interface

FunctionsDescriptions
abstract fun contains(element: E): Boolean It checks specified element is contained in this collection.
abstract fun containsAll(elements: Collection<E>): 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<E> It returns a list iterator over the elements in proper sequence in current list.
abstract fun listIterator(index: Int): ListIterator<E> 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).
  • Example 1: Let’s see an example of list using listOf() function.
fun main(args: Array<String>){
    var list = listOf("Ajay","Vishal","Prakash")//read only, fix-size
    for(element in list){
        println(element)
    }

Output :

Ajay
Vishal
Prakash
  • 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","Vishal","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
Vishal
Prakash
1
2
3
Ajay
Vishal
Prakash
  • 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","Vishal","Prakash")
    var anyList: List<Any> = listOf<Any>(1,2,3,"Ajay","Vishal","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
Vishal
Prakash
print any list
1
2
3
Ajay
Vishal
Prakash
  • 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","Vishal","Prakash","Vishal","Rohan")
    var list: List<String> = listOf<String>("Ajay","Vishal","Prakash")
    for(element in stringList){
        print(element+" ")
    }
    println()
    println(stringList.get(0))
    println(stringList.indexOf("Vishal"))
    println(stringList.lastIndexOf("Vishal"))
    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 Vishal Prakash Vishal Rohan
Ajay
1
3
5
true
true
[Prakash, Vishal]
false
[Vishal, Prakash, Vishal, Rohan]
[Ajay, Vishal, 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.

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.