How to convert an array of bytes to a hex string in Scala

If you need to convert an array of bytes to a hex string in Scala, I can confirm that this code works:

def convertBytesToHex(bytes: Seq[Byte]): String = {
    val sb = new StringBuilder
    for (b <- bytes) {

I just used this code as part of a checksum algorithm (SHA-1, SHA-256, etc.), and I tested it against command line checksum commands to verify that it works properly.

How to convert a Java Map to a Scala Map using JavaConverters

Here’s a quick look at how to convert a Java Map (such as HashMap) to a Scala Map using the JavaConverters object:

// import what you need
import java.util._
import scala.collection.JavaConverters._

// create and populate a java map
val jMap = new HashMap[String, String]()
jMap.put("first_name", "Alvin")
jMap.put("last_name",  "Alexander")

// convert the java map to a scala map
val sMap = jMap.asScala

Scala number, date, and formatting examples

This short blog post contains a collection of Scala number and date examples. I created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe the examples here much at all, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

Scala numeric types

Scala has these numeric types:

Scala “string to date” and “date to string” methods

Here are a couple of “string to date” and “date to string” methods. They’re written in Scala, but are easily converted to Java. They only use the Java Date and SimpleDateFormat classes:

A look at how the Scala `lazy val` syntax gets converted into Java code (bytecode)

Table of Contents1 - A little `lazy val` conversion example2 - A second `lazy val` conversion example3 - One more `lazy val` conversion example4 - The end

I don’t have any major conclusions to share in this blog post, but ... what I was curious about is how Scala implements lazy val fields. That is, when the Scala code I write is translated into a .class file and bytecode that a JVM can understand, what does that resulting code look like?