Scala Tutorial - Scala Tuples

A tuple is an ordered container of two or more values of same or different types.

Unlike lists and arrays, however, there is no way to iterate through elements in a tuple.

Its purpose is only as a container for more than one value.

Tuples are useful when you need to group discrete elements and provide a generic means to structure data.

We can create a tuple in two ways:

  • By writing your values separated by a comma and surrounded by a pair of parentheses
  • By using a relation operator ->


The following code shows a tuple containing an Int, a Boolean, and a String using the former method.

val tuple = (1, false, "Scala")

The following code shows a tuple created using a relation operator:

val tuple2 ="title" -> "Beginning Scala"

Individual elements of a tuple can be accessed by its index, where the first element has an index 1.

The following code shows accessing the third element of the tuple.

val tuple = (1, false, "Scala")
val third = tuple._3