key

How to sort data that’s in a Java HashMap (using a TreeMap) alvin June 29, 2019 - 12:32pm

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:

Scala SBT: Keys object fields alvin December 22, 2018 - 6:23pm

If/when you need to know what keys are available in a Scala SBT build.sbt file, you can find them all listed in the Keys object. At the current moment, here’s the Scaladoc for the SBT Keys object.

Note that the first two SBT keys you’ll probably see are name and scalaVersion.

How do I generate a secret key (application key) with Play Framework 2.6? alvin April 4, 2018 - 2:22pm

Play Framework FAQ: How do I generate a secret key (application key) with Play Framework 2.6?

Answer: Use the playGenerateSecret command in the SBT shell. Here’s an example:

$ sbt

[Scrupal6] $ playGenerateSecret
[info] Generated new secret: TozWYjreCna1QlVBGqfK4D/C6wZyQOm9gm<9;D_KO@]n
[success] Total time: 0 s, completed Apr 4, 2018 2:21:57 PM
How to iterate (loop) over the elements in a Map in Java 8 alvin February 5, 2017 - 3:44pm

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));
A Java tuple class (Tuple2 or Pair, if you prefer) alvin January 25, 2017 - 2:41pm

After working with Scala for a long time, I had to come back to Java for a while to work on an Android app. Right away I missed a lot of things from the Scala world, including all of the built-in Scala collection methods, and other things as simple as the Scala Tuple classes.

If you haven’t used them before, a Scala Tuple class lets you write code like this:

Tuple<String, Integer> t = new Tuple<>("age", 41);

If you’re comfortable with generics, the Java implementation of a Tuple class like this is simple:

How to find the largest key or value in a Scala Map alvin June 6, 2015 - 3:38pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.24, “How to Find the Largest Key or Value in a Scala Map”

Problem

You want to find the largest value of a key or value in a map.

Solution

Use the max method on the map, or use the map’s keysIterator or valuesIterator with other approaches, depending on your needs.

For example, given this map:

How to sort a Scala Map by key or value (sortBy, sortWith) alvin June 6, 2015 - 3:33pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.23, “How to Sort an Existing Map by Key or Value”

Problem

You have an unsorted map and want to sort the elements in the map by the key or value.

Solution

Given a basic, immutable Map:

How to test for the existence of a key or value in 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.21, “How to Test for the Existence of a Key or Value in a Scala Map”

Problem

You want to test whether a Scala Map contains a given key or value.

Solution

To test for the existence of a key in a Map, use the contains method:

How to access Map values in Scala alvin June 6, 2015 - 3:09pm

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:

Android: How to attach an extra to an Intent/PendingIntent in a Notification (solution) alvin February 3, 2015 - 9:43am

I’m working on a very small Android “notifications” app where I a) display a notification using a background service, b) the user taps the notification, which c) takes them to a view that shows the full text of the notification. To get this to work, I need to send the full text along with the notification. However, this didn’t work easily. The text that was shown by my full view would be updated once, then never again.

After a lot of googling and trial and error, I finally got this approach in my sendNotification method working: