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
- 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
- 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
- Here, in above example var (a, b, c)= (15, “chandan”, true) expression assign a = 15, b = “chandan”, c = true.
- 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
- 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)
- 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)
Please Login to comment...