Collection based Kotlin interview questions and answers
What is the difference between List and Array types? What is the difference between List and Array types?
The major difference from usage side is that Arrays
have a fixed size while (Mutable)List
can adjust their size dynamically. Moreover Array
is mutable whereas List
is not.
Furthermore kotlin.collections.List
is an interface implemented among others by java.util.ArrayList
. It's also extended by kotlin.collections.MutableList
to be used when a collections that allows for item modification is needed.
On the jvm level Array
is represented by arrays. List
on the other hand is represented by java.util.List
since there are no immutable collections equivalents available in Java.
What is basic difference between fold and reduce in Kotlin? When to use which? What is basic difference between fold and reduce in Kotlin? When to use which?
fold
takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters.listOf(1, 2, 3).fold(0) { sum, element -> sum + element }
The first call to the lambda will be with parameters
0
and1
.Having the ability to pass in an initial value is useful if you have to provide some sort of default value or parameter for your operation.
reduce
doesn't take an initial value, but instead starts with the first element of the collection as the accumulator (calledsum
in the following example)listOf(1, 2, 3).reduce { sum, element -> sum + element }
The first call to the lambda here will be with parameters
1
and2
.
What is the difference between FlatMap and Map in Kotlin?
- FlatMap is used to combine all the items of lists into one list.
- Map is used to transform a list based on certain conditions.
Read more about FlatMap and Map in Kotlin from MindOrks blog.
What is the difference between List and Array types in Kotlin?
If you have a list of data that is having a fixed size, then you can use an Array. But if the size of the list can vary, then we have to use a mutable list.
Learn more about the difference between List and Array from MindOrks blog.
What are collections in Kotlin?
val mutableList vs var immutableList. When to use which in Kotlin? ☆☆☆
Answer: Mutable and immutable list increase the design clarity of the model.
This is to force developer to think and clarify the purpose of collection.
- If the collection will change as part of design, use mutable collection
- If model is meant only for viewing, use immutable list
Purpose of val
and var
is different from immutable and mutable list.val
and var
keyword talk about the how a value/reference of a variable should be treated.
var
- value/reference assigned to a variable can be changed at any point of time.val
- value/reference can be assigned only once to a variable and can't be changed later point in the execution.
There are several reasons why immutable objects are often preferable:
- They encourage functional programming, where state is not mutated, but passed on to the next function which creates a new state based on it. This is very well visible in the Kotlin collection methods such as map, filter, reduce, etc.
- A program without side effects is often easier to understand and debug (you can be sure that the value of an object will always be the one at its definition).
- In multithreaded programs, immutable resources cannot cause race conditions, as no write access is involved.
You have also some disadvantages:
- Copying entire collections just to add/remove a single element is computationally expensive.
- In some cases, immutability can make the code more complex, when you tediously need to change single fields. In Kotlin, data classes come with a built-in copy() method where you can copy an instance, while providing new values for only some of the fields.
Source: stackoverflow.com
aaaaaa
Comments
Post a Comment