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 Kotlin Map 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")

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.

Scala for/yield examples (for-loop and yield examples)

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will be helpful to know how it works. Here's a statement of how the yield keyword works in for loops, based on the documentation in the book, Programming in Scala:

How to iterate (loop) over the elements in a Map in Java 8

If you need to iterate over the elements in a Map in Java 8, this source code shows how to do it:

Map<String, String> map = new HashMap<String, String>();
map.put("first_name", "Alvin");
map.put("last_name",  "Alexander");

// java 8
map.forEach((k,v)->System.out.println("key: " + k + ", value: " + v));

How to loop over a Scala collection with foreach

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.9, “How to loop over a Collection with foreach”.


You want to iterate over the elements in a collection with the foreach method.

How to process a Scala String one character at a time (with map, for, and foreach)

Scala FAQ: How can I iterate through each character in a Scala String, performing an operation on each character as I traverse the string?


Depending on your needs and preferences, you can use the Scala map or foreach methods, a for loop, or other approaches.

The map method

Here’s a simple example of how to create an uppercase string from an input string, using the map method that’s available on all Scala sequential collections:

A simple Arduino Uno LED fade program

Shortly after writing my Arduino Uno Hello, World example, I read my documentation a little more, learned that Pin 9 on the Arduino Uno board is an analog pin, switched the LED wire to that pin, and created the following program, which slowly lights up the LED using an analog programming approach, and then fades the LED light out.

Here's the source code for my LED fade sketch:

The AppleScript for loop (and while loop) examples

AppleScript for loop FAQ: How do I use an AppleScript for loop? (Also, how do you use an AppleScript while loop?)

This is actually a bit of a trick question, as there is no AppleScript for loop or while loop syntax. Instead you use the AppleScript repeat command, as shown in the following examples.

AppleScript for loop examples

Where you might expect an AppleScript for loop to iterate over a list, you use the AppleScript repeat with syntax: