Skip to content
Related Articles
Open in App
Not now

Related Articles

Scala | Tuple

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 06 Mar, 2023
Improve Article
Save Article
Like Article

Tuple is a collection of elements. Tuples are heterogeneous data structures, i.e., is they can store elements of different data types. A tuple is immutable, unlike an array in scala which is mutable. An example of a tuple storing an integer, a string, and boolean value.

val name = (15, "Chandan", true)

Type of tuple is defined by, the number of the element it contains and datatype of those elements. For Example:

// this is tuple of type Tuple3[ Int, String, Boolean ] 
val name = (15, "Chandan", true)

Let N be the number of elements in a tuple. Scala currently is limited to 22 elements in a tuple, that is N should be, 1<=N<=22 , the tuple can have at most 22 elements, if the number of elements exceeds 22 then this will generate an error. However we can use nested tuples to overcome this limit (Note that a tuple can contain other tuples)

Operations on tuple
  1. Access element from tuple: Tuple elements can be accessed using an underscore syntax, method tup._i is used to access the ith element of the tuple. Example : 

Scala




// Scala program to access
// element using underscore method
 
// Creating object
object gfg
{
    // Main method
    def main(args: Array[String])
    {
 
        var name = (15, "chandan", true)
 
        println(name._1) // print 1st element
        println(name._2) // print 2nd element
        println(name._3) // print 3st element
    }
}


Output:

15
chandan
true
  1. Pattern matching on tuples : Pattern matching is a mechanism for checking a value against a pattern. A successful match can also deconstruct a value into its constituent parts. Example : 

Scala




// Scala program of pattern matching on tuples
 
// Creating object
object gfg
{
    // Main method
    def main(args: Array[String])
    {
        var (a, b, c) = (15, "chandan", true)
        println(a)
        println(b)
        println(c)
    }
}


Output:

15
chandan
true
  1. Here, in above example var (a, b, c)= (15, “chandan”, true) expression assign a = 15, b = “chandan”, c = true.
  2. Iterating over a tuple : To iterate over tuple, tuple.productIterator() method is used. Example : 

Scala




// Scala program to iterate over tuples
// using productIterator method
 
// Creating object
object gfg
{
    // Main method
    def main(args: Array[String])
    {
        var name = (15, "chandan", true)
         
        // The foreach method takes a function
        // as parameter and applies it to
        // every element in the collection
        name.productIterator.foreach{i=>println(i)}
    }
}


Output:

15
chandan
true
  1. Converting tuple to string: Converting a tuple to a string concatenates all of its elements into a string. We use the tuple.toString() method for this. Example : 

Scala




// Scala program to convert tuple element to String
 
// Creating object
object gfg
{
    // Main method
    def main(args: Array[String])
    {
        val name = (15, "chandan", true)
         
        // print converted string
        println(name.toString() )
    }  
}


Output:

(15, chandan, true)
  1. Swap the elements of tuple: Swapping the element of a tuple we can use tuple.swap Method. Example : 

Scala




// Scala program to swap tuple element
 
// Creating object
object gfg
{
    // Main method
    def main(args: Array[String])
    {
        val name = ("geeksforgeeks","gfg")
         
        // print swapped element
        println(name.swap)
    }
}


Output:

(Geeksquize,geeksforgeeks)

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!