values

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:

Functional programming: Math functions, not programming functions

“There’s only ONE rule, but it’s an important one: all of your values must be functions. Not programming functions, but math functions.”

I think I read that quote in an earlier version of this article. The quote is about functional programming, and it influenced something I wrote in my book, Functional Programming, Simplified: Functional programmers think about themselves as being mathematicians, and think of their code as being a combination of algebraic equations, where each function is a pure function that you can think of in mathematical terms.

Erlang has single-assignment variables

“Erlang has single-assignment variables. As the name suggests, they can be given a value only once. If you try to change the value of a variable once it has been set, you’ll get an error.”

(“Single-assignment variables” are the same as val fields in Scala. Using them can make your code more like algebra.)

You are free to choose your own vision

In daily life, each of us is a vehicle for something. Our choices of values determine the kinds of vehicles we are, the way we move in the world and relate to each other. All individuals (and organizations) are free to choose values that they feel are important, that express their vision.

(A quote from the book, Zen at Work.)

My oldest sister is five years older than I am, and when she was in high school she was involved in activities outside of school, like the foreign student exchange program. I was an extreme introvert and could never imagine myself doing those things, but she was very involved in making our community a better place, and I was proud of her.

Back then I didn’t know that I was free to choose my own vision ... the mental weight of being an extreme introvert kept me from seeing that. I may always struggle with being an introvert, but these days I understand that I can choose my own vision, and I do so consciously.

What are the Drupal 8 Node class fields (field names)?

I was just trying to modify one of my Drupal 8 template files — node.html.twig — and I couldn’t find any good documentation for what variables/values/fields are in the Drupal 8 Node class, so I dumped some output to my browser, and saw that these are the Node fields:

How to reverse keys and values in a Scala Map

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one the smallest recipes, Recipe 11.20, “How to Reverse Keys and Values in a Scala Map”

Problem

You want to reverse the contents of a Scala Map, so the values become the keys, and the keys become the values.

Solution

You can reverse the keys and values of a map with a for-comprehension, being sure to assign the result to a new variable:

How to get the keys or values from a Scala Map

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 11.19, “How to Get the Keys or Values from a Scala Map”

Problem

You want to get all of the keys or values from a Scala Map.

Solution

To get the keys, use keySet to get the keys as a Set, keys to get an Iterable, or keysIterator to get the keys as an iterator:

How to access Map values in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 11.17, “How to Access Map Values in Scala”

Problem

You want to access individual values stored in a map. You may have tried this and run into an exception when a key didn’t exist, and want to see how to avoid that exception.

Solution

Given a sample map: