tuple

A Java tuple class (Tuple2 or Pair, if you prefer)

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 write a Scala function that returns multiple values

As a quick note today, if you want to write a Scala function that returns multiple values, just return the values inside a tuple. For example, I just wrote a function to return radio station number and name, and the last part of that function looks like this:

def getRadioStationInfo(...) = {
   ...
   (104.3, "The Fan")
}

The two values are returned in an instance of a Scala Tuple2 class.

How to define Scala methods that return multiple items (tuples)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 5.5, “How to define Scala methods that returns multiple items (tuples).”

Problem

You want to return multiple values from a method, but don’t want to wrap those values in a makeshift class.

Solution

Although you can return objects from methods just as in other OOP languages, Scala also lets you return multiple values from a method using tuples. First, define a method that returns a tuple:

How to use pattern matching in Scala match/case expressions

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.11, “How to use pattern matching in Scala match expressions.”

Problem

You need to match one or more patterns in a match expression, and the pattern may be a constant pattern, variable pattern, constructor pattern, sequence pattern, tuple pattern, or type pattern.

Scala Tuples, for when you just need a bag of things

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.27, “Scala Tuples, for When You Just Need a Bag of Things”

Problem

You want to create a small collection of heterogeneous elements.

Solution

A tuple gives you a way to store a group of heterogeneous items in a container, which is useful in many situations.

Create a tuple by enclosing the desired elements between parentheses. This is a two- element tuple:

Scala tuple examples and syntax

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:

Scala - Using tuples in an anonymous function

I just ran across this scala-lang.org post about using tuples in an anonymous function, and thought it was good enough to reproduce here, with only the solution part.

In essence, the question is, if I have a Map like this, which is really composed of tuples:

A simple Java Generics example class

To take a break from a project I was working on yesterday I gave my brain a little exercise to see if I could remember how to create a Java class that used the Java 5 Generics magic. Since I’ve used Scala a lot recently, I decided to create a Java version of the Scala Tuple class.

The Tuple class simply stores two values, which are often key/value pairs. Beginning with the end in mind, here’s how you would typically use a Tuple in Java (if it existed):