list-collection-kotlin

List in Kotlin

List is an ordered collection and can contain duplicate elements. You can access any element from its index.

List is more like array with dynamic length, List is one of the most used Collection type.

In my language : List is nothing but a library, what do you think about 'How the books are stored in the Library'

In any Library in the work they will store it in the racks which have some ISBN or some unique number.

If you ask a Librarian to bring a book which is in '12371' rack, now librarian will go to that rack and fetches the book.

Probably if you ask he may not do, but he will fetch if someone from higher official asks.

Library can hold more number of same book , isn't it ?, because it is stored in a rack which has unique numbers.

Similar to library the list collection also stores the objects based on the index number, and it can also store duplicate objects as the access is going to happen through index.

List is a interface, and it just provides what should be present if somebody implements the interface, so the list interface will not have any implementations.

ArrayList and LinkedList classes provide implementation to the List interface methods, the methods present in the ArrayList and LinkedList are same with little changes

Kotlin List functions

[ ]

[](index) operator is used to get a particular element in the list with given index, the index of the kotlin collections & arrays starts with 0

fun main(args: Array<String>) {
    var sampleList = listOf(8, 6, 5, 1)
    println(sampleList[2])
}

get()

get() method is also helps you to retrieve the value from the list, get() method return a value with return type

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1)
    println(abc.get(2))
}

indexOf()

indexOf() method gets the position of the given value, if the given value is not present then kotlin returns -1. indexOf() returns the position of the first occurrence.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1)
    println(abc.indexOf(6))
}

lastIndexOf()

lastIndexOf() function returns the last positions of the given value, if given value is not present then kotlin will return -1

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.lastIndexOf(6))
}

Set Collections in Kotlin

last()

last() method will return the value of the last element in the given list in kotlin

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.last())
}

subList()

subList() function will return a list with the given limits from the Original list, lower limit is included and upper limit is not included.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.subList(1, 4))
}

size

size property (it is not a function) returns the number of elements/items present in the list.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.size)
}

contains()

contains() method will return true if the given value is present in the list otherwise false.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.contains(8))
    println(abc.contains(108))
}

containsAll()

containsAll() function accepts a list of elements and check whether the given sub list items are present in the Original list irrespective of order.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.containsAll(listOf(1,6)))
    println(abc.containsAll(listOf(5,8)))
    println(abc.containsAll(listOf(5,33)))
}

isEmpty()

isEmpty() function checks whether th list has any element or not, if there is no element is present then kotlin will return true otherwise false.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.isEmpty())
}

Map Collections in Kotlin

asReversed()

asReversed() function return a list with reversed values in the original list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.asReversed())
}

dropLast()

dropLast() remove the given number of elements from the list starting at the end, for example if you pass 3 as value to dropLast() function thn last three element from the list will be dropped and return the remaining elements. It does not affect the original list

If the given number id more than the size of the list then it will remove all values but it will not throw any error

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.dropLast(1))
    println(abc)
}

drop()

drop() function removes the given number of elements from the list starting at the beginning of the list, for example if you pass 3 as value to drop() function then, first three element from the list will be dropped and return the remaining elements. It does not affect the original list

If the given number id more than the size of the list then it will remove all values but it will not throw any error

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.drop(2))
    println(abc)
}

elementAt()

elementAt() function will return a value at the given index, if the given index is not present(i.e more than size of the list) then kotlin will throw Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.elementAt(20))
}

elementAtOrElse()

elementAtOrElse() function will return a value at the given index, if the given index is not present then instead of raising the ArrayIndexOutOfBoundsException kotlin will return the given lambda result

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.elementAtOrElse(1, {999}))
    println(abc.elementAtOrElse(35, {999}))
}

elementAtOrNull()

elementAtOrNull() function will return the value at the given index, if given index is not present then kotlin will return null instead of raising ArrayIndexOutOfBoundsException

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.elementAtOrNull(1))
    println(abc.elementAtOrNull(33))
}

first()

first() function will return first element of the list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.first())
}

firstOrNull()

firstOrNull() function will return first element of the list, if the list empty then kotlin ill return null

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.firstOrNull())
}

Manipulate Collections

getOrElse()

getOrElse() method try to retrieve the value from the given index from the list, if the give index is not there then kotlin will result the default value that we have given in the form of lambda

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.getOrElse(3, {888}))
	println(abc.getOrElse(67, {888}))
}

getOrNull()

getOrElse() method try to retrieve the value from the given index from the list, if the give index is not there then kotlin will return null.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 5, 1, 6)
    println(abc.getOrNull(3))
    println(abc.getOrNull(44))
}

single()

single() function returns the single element, or throws an exception if the list is empty or has more than one element.

The exception would be Exception in thread "main" java.lang.IllegalArgumentException: List has more than one element.

fun main(args: Array<String>) {
    var abc = listOf(8)
    println(abc.single())
    var xyz = listOf(8, 3, 5)
    println(xyz.single())
}

singleOrNull()

singleOrNull() function returns single element, or 'null' if the list is empty or has more than one element.

fun main(args: Array<String>) {
    var abc = listOf(8)
    println(abc.singleOrNull())
    var xyz = listOf(8, 3, 5)
    println(xyz.singleOrNull())
}

slice()

slice() function returns a list containing elements at indices in the specified indices range, .. operator is used to generate the range

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.slice(1..3))
}

takeLast()

takeLast() function returns a list containing last [n] elements. n is given value

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.takeLast(3))
}

any()

any()function will return true if the given list has at least one element otherwise false.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.any())
}

average()

average() function returns an average value of elements in the collection, add all elements in the list and divide the result with the number of elements present in the list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.average())
}

isNotEmpty()

isNotEmpty() function return true if the given list is not empty, that means if list has at least one element then result would be true otherwise false

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.isNotEmpty())
}

count()

count() function returns the number of elements in this collection.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 9, 5, 7)
    println(abc.count())
}

distinct()

distinct() return the unique values present in the list which mean if a element is present more than once then result will have the element once only

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.distinct())
}

max()

max() function return maximum value present in the list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.max())
}

min()

min() function returns minimum value present in the list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.min())
}

plusElement() & plus()

Returns a list containing all elements of the original collection and then the given [element]. These methods does not alter the existing list

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.plusElement(20))
	println(abc)
}

reversed()

reversed() function returns a list with elements in reversed order.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.reversed())
    println(abc)
}

shuffled()

shuffled() function returns a new list with the elements of this list randomly shuffled

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.shuffled())
    println(abc)
}

sorted()

sorted() function sorts the elements present in the list according to ascending order

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.sorted())
    println(abc)
}

sum()

sum() function adds all the elements and return the result

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.sum())
    println(abc)
}

Reflection in kotlin

toIntArray()

toIntArray() function returns an array of Int containing all of the elements of this collection.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.toIntArray())
}

toSet()

toSet() function returns a Set of all elements.

fun main(args: Array<String>) {
    var abc = listOf(8, 6, 3, 8, 6, 7)
    println(abc.toSet())
}						

aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group