map

How to create a large test Map in Scala (converting a sequence to a map)

If you ever need to create a large test Scala Map, I just used this approach and it worked fine:

val map = (for (i <- 1 to 5_000_000) yield (i, 10*i)).toMap

The initial results in the Scala REPL look like this:

val map: Map[Int, Int] = HashMap(4584205 -> 45842050, 2231874 -> 22318740, ...

The code works by creating a series of tuples with the yield expression, where each key is i, and each value is 10*i. Here are a couple of other ways to convert a Scala sequence to a map:

val map = Vector.range(0,1_000_000).map(i => (i, i*10)).toMap
val map = Vector.range(0,1_000_000).map(i => i -> i*10).toMap

Think of the Scala collections’ map method as “transform”

I’ve written this before, but when I saw this “pseudocode to Scala code” example in the book Functional Thinking, I thought it was worth mentioning again: If you have trouble grokking the Scala map method, think of it as being named transform instead. It transforms an input collection to an output collection, based on the algorithm you supply.

For those coming from the OOP world, I think “transform” is a better word because it is more meaningful, at least initially.

Scala immutable Map class: methods, examples, and syntax

This page contains a large collection of examples of how to use the Scala Map class. There are currently well over 100 examples.

A Scala Map is a collection of unique keys and their associated values (i.e., a collection of key/value pairs), similar to a Java Map, Ruby Hash, or Python dictionary.

How to sort data that’s in a Java HashMap (using a TreeMap)

Here’s a brief example of how to sort data that’s already in a Java HashMap. First, some HashMap data:

import java.util.*;

Map<Integer, Float> playMap = new HashMap<Integer, Float>() {{
    put(10,  1f);
    put(12,  2f);
    put(15,  4f);
    put(18,  6f);
    put(20,  6f);
    put(25,  7f);
    put(30,  6f);
    put(35,  5f);
    put(40,  5f);
}};

A HashMap won’t keep that data in sorted order, so if you want your data sorted, one way to sort it is to add the elements to a Java TreeMap:

A Java JFreeChart x/y plot/chart/graph example

In an effort to share some source code (but without taking the time to explain it), here’s some Java source code that I just used to create a JFreeChart chart/graph of some data that I use in my Android football game:

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")
}
Best RxJava links: map, flatMap, concatMap, and more alvin March 28, 2019 - 2:28pm

As often happens, I have about 50 browser tabs open, and in an effort to close some of those down, these are some of the best links I found while working with RxJava a week or two ago:

Kotlin collections methods: examples and syntax

As a quick note today, if you ever need some examples of how the Kotlin collections methods work, I hope these examples are helpful.

Sample data

First, here’s some sample data:

val a = listOf(10, 20, 30, 40, 10)
val names = listOf("joel", "ed", "chris", "maurice")