Kotlin Common Questions

  1. Home
  2. chevron_right
  3. Kotlin
  4. chevron_right
  5. Kotlin Common Questions

1. Where should we keep constants in Kotlin?

  • if you need constants to be accessed globally use const as top-level properties (not inside a class).

In order to do this create a Kotlin file named Constants.kt. The file would look like this:

This way you will be able to access the constants from anywhere within the app just by typing the constant name (in Kotlin code). In Java code you will need to write ConstantsKt.MY_APP_ID.

  • if you have constants that are specific to a scope, like Employee for example, create a companion object
package ro.tutorial.funcode.kotlinexercise

open class Employee(open val id: Int, val name: String) {
    companion object {
        const val MAX = 5
    }
}

And this is how you use the constants.

2. What do ?, ?., ?: and !! mean?

3. How to make casts in Kotlin?

val listView = findViewById(R.id.listView) as ListView

4. How to use Parcelable in Kotlin?

5. How to create a Singleton in Kotlin?

6. How to create a List in Kotlin?

List

List

  • Create a List in Kotlin

val list: List<String> = listOf("Apple", "Cherry", "Carrot")
Important!

In Kotlin, lists are immutable by default (you can’t add or remove elements after the list is created). If you need to make a list mutable, you can do this by using mutableListOf

val list: List<String> = mutableListOf<>("Apple", "Cherry", "Carrot")
list.add("Cucumber")

OR

by creating an ArrayList.

val list: ArrayList<String> = arrayListOf("Apple", "Cherry", "Carrot") //mutable

BUT it should be just a temporary list. It is not recommended to keep mutable lists for too long.

  • Mixed types

When you create a list in Kotlin, you can add mixed types to the same list:

 val list = listOf("Apple", "Cherry", 100, true)
  • listOfNotNull() Function

val fruit = null
val list = listOfNotNull("Apple", "Cherry", fruit)

for (i in list) {
   println(i)
}

The output of the above code will be this:

Apple

Cherry

Notice that null values are ignored.

  • Empty lists

There are 2 ways to create empty lists. They do the same thing, so it’s up to you which way you prefer.

val list = listOf<String>()

OR

val list = emptyList<String>()

emptyList<String>() returns an Immutable empty list.

7. How to create a SET in Kotlin?

  • Create a Set in Kotlin

val set = setOf("Apple", "Cherry", "Carrot") //immutable
  • Create a HashSet

val set = hashSetOf("Apple", "Cherry", "Carrot") //mutable
  • Create a SortedSet (TreeSet in Java)

val set = sortedSetOf("Apple", "Cherry", "Carrot") //mutable
  • Create a LinkedSet (LinkedHashSet in Java)

val set = linkedSetOf("Apple", "Cherry", "Carrot") //mutable

 

8. How to create a Map in Kotlin?

  • Create a Map

val map = mapOf(1 to "John", 2 to "Chloe", 3 to "Maria") //immutable
  •  How to iterate a Map?

val map = mapOf(1 to "John", 2 to "Chloe", 3 to "Maria")

for ((key, value) in map) {
     println("$value has the id = $key")
}

HashMap, LinkedMap (LinkedHashMap in Java) and SortedMap are all mutable.

9. What collection methods are useful?

any()

Returns true if collection has at least one element.

val list = listOf("Apple", "Cherry", "Carrot")
println("The list has at least one element = ${list.any()}") //returns TRUE

any(predicate: (T) -> Boolean

Returns true if at least one element matches the given predicate.

val list = listOf("Apple", "Cherry", "Carrot")
println("The list has at least one element of 15 letters = ${list.any{it.length == 15}}") //returns FALSE

 

asReversed()

Returns a reversed read-only view of the original List.

val list = listOf("Apple", "Cherry", "Carrot")
println(list.asReversed())

Output

I/System.out: [Carrot, Cherry, Apple]

 

Menu