kotlin

How to iterate over a Map in Kotlin (for loop)

Kotlin FAQ: How do I iterate over a Map in Kotlin?

Solution: Here’s an example of how to iterate over a Map in Kotlin using a for loop:

val map = mapOf("a" to 1, "b" to 2, "c" to 3)

for ((k,v) in map) {
    println("value of $k is $v")
}

Examples of how to use 'let' in Kotlin

As a little note to self, here are a few let examples in Kotlin:

fun main(args: Array<String>) {

    fun toUpper(s: String?): String? = s?.toUpperCase()

    toUpper(null)?.let {
        // this will never be run
        println("toUpper(null) is: ${it}")
    }

    toUpper("emily")?.let {
        println("toUpper(emily) is: ${it}")
    }

    // `let` can return a value
    val hello = toUpper("Hannah")?.let {
        "Hello, ${it}"
    }
    println(hello)

}

The output looks like this:

A Kotlin Adler-32 checksum algorithm

As a short post today, here’s an example of a Kotlin implementation of the Adler-32 checksum algorithm:

“Kotlin Quick Reference” book

Table of Contents1 - Kotlin Quick Reference2 - It’s free3 - It’s open source4 - A caveat5 - Contributing6 - The Kindle version7 - Downloads

Somewhere in mid-2017 I started working on a Kotlin programming book, but then I had to get away from it to work on other things. When I got back to it recently I looked around and felt like the world didn’t need another “Introduction to Kotlin” book — there are a couple of good ones out there, including Kotlin in Action, and the kotlinlang.org documentation is excellent — so I decided to ditch the project completely.

Back to top

Kotlin Quick Reference

But then when I started writing some Kotlin code again I realized that what I really needed was a quick reference. I didn’t want to have to dig through a tutorial book or website to find what I need, I just wanted something like a large cheat sheet where I could quickly find the Kotlin syntax and examples for whatever I was working on at that moment. So I decided to strip down what I had already written and create both a book and a Kotlin Quick Reference website.

Kotlin: A for loop that counts up to some maximum integer value

As a brief note to self, I was just converting some Java code to Kotlin, and the correct way to convert this Java for loop that uses i as a counter:

for (int i=0; i<tabLayout.tabCount; i++) { ...

is with this Kotlin for loop:

for (i in 0 until tabLayout.tabCount) { ...

The key there for me is the 0 until part of the syntax.

Android/Kotlin: A FloatingActionButton setOnClickListener Snackbar example

As a quick note to self, this is an example of how to implement a setOnClickListener on a FloatingActionButton with Android and Kotlin:

fab.setOnClickListener { view ->
    Snackbar.make(
        view,
        "An exciting message!",
        Snackbar.LENGTH_LONG
    ).setAction("Action", null).show()
}

Hopefully that’s enough to get “future me” pointed in the right direction when I need this again. :)

(There are more lambda details at this link.)

How to generate a small random number in Kotlin

If you need to generate a small random number in Kotlin, I find that this approach works:

val r = (1..10).shuffled().first()

That code generates a single random number in the range from 1 to 10 (including 1 on the low end and 10 on the high end).

You can use the technique to simulate the rolling of a dice (die):

val r = (1..6).shuffled().first()

Or simulate flipping a coin (0=heads, 1=tails, or vice versa):

val r = (1..2).shuffled().first()

This also works for the coin flip simulation: