Scala | Tuple

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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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 2st element
            println(name._3) // print 3st element
        }
    }

    chevron_right

    
    

    Output:

    15
    chandan
    true
    
  2. 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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    15
    chandan
    true
    

    Here, in above example var (a, b, c)= (15, “chandan”, true) expression assign a = 15, b = “chandan”, c = true.

  3. Iterating over a tuple : To iterate over tuple, tuple.productIterator() method is used.
    Example :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)}
        }
    }

    chevron_right

    
    

    Output:

    15
    chandan
    true
    
  4. 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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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() )
        }   
    }

    chevron_right

    
    

    Output:

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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)
        }
    }

    chevron_right

    
    

    Output:

    (Geeksquize,geeksforgeeks)
    


  6. My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :

    1


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.