# 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 alvin July 28, 2018 - 7:53pm

“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.

This is a page from my book, Functional Programming, Simplified

## Functions Are Variables, Too

“A variable is a named entity that refers to an object. A variable is either a `val` or a `var`. Both vals and vars must be initialized when defined, but only vars can be later reassigned to refer to a different object.”

The Scala Glossary

## 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: