Tuples
Last updated
Was this helpful?
Last updated
Was this helpful?
Tuples are ordered sets of values; you can think of tuples like records in a table in a relational database, where each element or value can be referenced by position.
gives you a way to store a group of heterogeneous items in a container. Create a tuple by enclosing the desired elements between parentheses. This is a two element tuple:
scala> val d = ("Debi", 95)
d: (String, Int) = (Debi,95)
scala> case class Person(name: String)
defined class Person
scala> val t = (3, "Three", new Person("Al"))
t: (Int, java.lang.String, Person) = (3,Three,Person(Al))
access tuple elements using an underscore construct:
scala> t._1
res1: Int = 3
scala> t._2
res2: java.lang.String = Three
scala> t._3
res3: Person = Person(Al)
The position of the value in the tuple has some relevance, unlike the position of an element in a list. Tuples can contain objects with a mixture of data types. Also like lists, tuples are immutable data structures.
Note in the last example that I used the class name Tuple3; if the tuple contained four elements, I would use the class name Tuple4, and so on.
After you have created a tuple, you can access any of the fields positionally by using _<field_no>; unlike lists or arrays, which are zero-based (meaning 0, 1, 2), element position in a tuple is one-based (meaning 1, 2, 3).
Tuples can be embedded in other types; for instance, it is sometimes useful to have a list of tuples or an
array of tuples.
A two-element tuple is an instance of the Tuple2 class, and a tuple with three elements
is an instance of the Tuple3 class.