This is Part 2 of Kotlin for Interviews. This series describes Kotlin’s functions and code snippets that came up frequently while preparing for an Android interview. We have also created a cheat sheet that covers all five parts of this series. Here..

Part 1: You can find common data types Here, Part 3: Numbers and Mathematics Here, Part 4: Iteration and Part Here And Part 5: Frequently used code snippets Here..

The contents of this part are as follows:

The Kotlin Standard Library provides many functions for performing operations on collections.I’ll only cover the ones I use most often in interview issues, but here’s Great article It gives an overview of all of them.

`get(key: V)`

`someCollection[key]`

As a getter.`getOrDefault(key: K, defaultValue: V)`

`getOrNull(index: Int)`

`null`

If the index is out of range. This is useful when working with arrays and lists.

`first(predicate: (T) -> Boolean): T`

Returns the first element that matches the specified predicate. If no element matches the predicate, an exception will be thrown.`firstOrNull(predicate: (T) -> Boolean): T?`

And`find(predicate: (T) -> Boolean): T?`

Both return the first element that matches the specified predicate, or`null`

If no such element is found. They can be used interchangeably.

*// Example of first()*

**val numbers = listOf("one", "two", "three", "four", "five", "six")**

**numbers.first { it.length > 3 }** *// returns “three”*

**numbers.first { it.length > 8 }** *// throws NoSuchElementException*

**numbers.firstOrNull { it.length > 8 }** *// returns null*

**numbers.find { it.length > 8 }** *// returns null*

`last(predicate: (T) -> Boolean): T`

Returns the last element that matches the specified predicate. If no element matches the predicate, an exception will be thrown.`lastOrNull(predicate: (T) -> Boolean): T`

`findLast(predicate: (T) -> Boolean): T`

`null`

If no such element is found. They can be used interchangeably.`indexOf(element: T): Int`

Returns the first index of the element, or`-1`

If the array contains no elements.`lastIndexOf(element: T): Int`

Returns the last index of the element, or`-1`

If the array contains no elements.`indexOfFirst(predicate: (T) -> Boolean): Int`

Returns the index of the first element that matches the specified predicate, or`-1`

If the array contains no elements.`indexOfFirst(predicate: (T) -> Boolean): Int`

Returns the index of the last element that matches the specified predicate, or`-1`

If the array contains no elements.`binarySearch(element: T?): Int`

Finds the element in the sorted collection and returns its index.`binarySearchBy(key: K?, selector: (T) -> K?): Int`

Finds the element whose key returned by the specified selector function is equal to the specified key value and returns its index.

// Example of binarySearchBy()

data class Box(val number: Int)val numbers = listOf(Box(1), Box(3), Box(7), Box(10), Box(12))// returns 3 because the element at index 3 has it.number == 10.boxes.binarySearchBy(10) { it.number }

`sort()`

`sorted(): List<T>`

Sorts in ascending order using the natural order of the elements and returns the result.`sortBy(selector: (T) -> R?)`

`sortedBy(selector: (T) -> R?): List<T>`

`sortWith(comparator: Comparator<in T>)`

`compareBy()`

Functions to help you create`Comparator`

Easy object.`sortedWith(comparator: Comparator<in T>): List<T>`

`sortDescending()`

*,*`sortedDescending()`

*,*`sortByDescending()`

*,*And`sortedByDescending()`

It has the same effect as the above function, but in descending order.

// Need to use a mutableList since sort() mutates the listval sortNumbers = mutableListOf(1, 2, 7, 6, 5, 6)

sortNumbers.sort()// sortNumbers becomes [1, 2, 5, 6, 7]val sortValues = mutableListOf(1 to "a", 5 to "c", 2 to "c", 7 to "b", 6 to "d")

sortValues.sortBy { it.second }// sortValues becomes [(1, a), (7, b), (5, c), (2, c), (6, d)]// sort by letter, and then by numbersortValues.sortWith(compareBy({it.second}, {it.first}))// sortValues becomes [(1, a), (7, b), (2, c), (5, c), (6, d)]

Too many different sort functions can be confusing, so I created the following table to summarize them.