Scala, Java, Unix, MacOS tutorials (page 309)

Scala/Java integration problem: You need to share a Scala map with a Java method, or access a Java map in Scala code.

Solution

To use a Java map in Scala code, import the mapAsScalaMap method from Scala’s JavaConversions package, and perform the conversion.

Simply trying to use a Java HashMap with something like a Scala for loop won’t work. To demonstrate this, first create a simple Java HashMap:

Scala FAQ: Can you share some examples of using tuples in Scala?

A Scala tuple is a class that can contain a miscellaneous collection of elements. I like to think of them as a little bag or container you can use to hold things and pass them around.

You create a tuple with the following syntax, enclosing its elements in parentheses. Here's a tuple that contains an Int and a String:

[toc hidden:1]

Scala String problem: You need to find the difference between two strings, the common characters between two strings, or the unique characters in a string.

Solution

You can perform all of these operations with built-in methods. Use the diff method to find the differences between two strings:

When coming to Scala from Java, the syntax for accessing a character at a position in a Scala String is an interesting thing. You could use the Java charAt method:

scala> "hello".charAt(0)
res0: Char = h

However, the preferred (proper) approach to access a character in a String is to use Scala’s Array notation:

Scala String FAQ: How do I convert a String to uppercase or lowercase, or capitalize the first character in the string?

Solution

To convert a String to uppercase or lowercase, use the toUpperCase and toLowerCase methods:

The reduceLeft method on the Scala collections is fun. Just start with a collection:

scala> val a = Array(20, 12, 6, 15, 2, 9)
a: Array[Int] = Array(20, 12, 6, 15, 2, 9)

Then give reduceLeft a simple function to work with, and let it do its thing:

Scala FAQ: How can I pass an array back and forth between Scala and Java code?

Solution

The first link in the See Also section states, “Scala arrays correspond one-to-one to Java arrays,” and as a result of this, passing arrays back and forth between Java and Scala code is easier than the example shown in Recipe 17.1 in the Scala Cookbook, “Going to and from Java Collections.”

Scala FAQ: How can I dynamically generate XML from my Scala source code, such as creating output for a SOAP web service?

Solution

A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.

To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:

Wow, it’s not even published yet, and people are searching for Scala Cookbook PDF download. That kind of freaks me out.

From the “Stuff I post on Facebook” category ...

Nothing better than a “bear walks into a bar” story ... (Estes Park, Colorado)

Okay, I’ve tried converting my free Play Framework Recipes booklet into an Amazon Kindle format, and in short, it’s going to take a fair amount of work to get it right. (The problem has to do with fixed-width fonts, i.e., all of the source code examples.)

Here’s the cover for my new, free PDF, Play Framework Recipes. (I obviously didn’t put much work into the cover, lol.) As the title implies, the book is a collection of cookbook-style recipes for the Play Framework. The book is about 80 pages in length, is currently available as a PDF, and will soon be available as a Kindle eBook. Here’s a link to more information on Play Framework Recipes.

Update, August 12, 2013: The second version of Play Framework Recipes is now available as a free PDF. It contains new appendices on Play commands, a JSON reference, improved formatting, and over 80 pages of Play Framework content. Get it from the Download section below.