If You are interested to learn about the scala Queue
A tuple is a collection of elements in ordered form. If there is no element present, it is called empty tuple. You can use tuple to store any type of data. You can store similar type or mix type data also. You can use it to return multiple values from a function. Immutable tuples means the object cannot change its value once assigned. With the use of tuple, we can group different types of data together which can be easy to access later. Like we have student class where we can store its name, age, address and id into a one single data structure called tuples.
Syntax & parameters:
var variable_name = (Data_type1, Data_type1, Data_type1, Data_type1..so on...)
Scala Tuple Example
In the following example, we have created tuple of different types of elements.
object MainObject{ def main(args:Array[String]){ var tuple = (1,5,8,6,4) // Tuple of integer values var tuple2 = ("Apple","Banana","Gavava") // Tuple of string values var tuple3 = (2.5,8.4,10.50) // Tuple of float values var tuple4 = (1,2.5,"India") // Tuple of mix type values println(tuple) println(tuple2) println(tuple3) println(tuple4) } }
Output:
(1,5,8,6,4)
(Apple,Banana,Gavava)
(2.5,8.4,10.5)
(1,2.5,India)
Scala Tuple Example: Accessing Tuple Elements
In this example, we are accessing tuple elements by using index. Here, we are using product Iterator for iterating tuple elements.
object MainObject{ def main(args:Array[String]){ var tupleValues = (1,2.5,"India") println("iterating values: ") tupleValues.productIterator.foreach(println) // Iterating tuple values using productIterator println("Accessing values: ") println(tupleValues._1) // Fetching first value println(tupleValues._2) // Fetching second value } }
Output:
iterating values:
1
2.5
India
Accessing values:
1
2.5
Scala Tuple Example: Function Return Multiple Values
You can return multiple values by using tuple. Function does not return multiple values but you can do this with the help of tuple. In the following example, we are describing this process.
object MainObject{ def main(args:Array[String]){ var tupleValues = tupleFunction() println("Iterating values: ") tupleValues.product Iterator.foreach(println) // Iterating tuple values using productIterator } def tupleFunction()={ var tuple = (1,2.5,"India") tuple } }
Output:
Iterating values:
1
2.5
India
How to Convert Scala Tuple to String
Swapping of Scala Tuple Elements
We can perform swapping of tuple elements by using tuple.swap method.
Example
[php]val t = new Tuple2(“tuple”, “test”) println(“Swapped Tuple: ” + t.swap )[/php]
Output
Swapped tuple: (test,tuple)
Converting Tuple to String in Scala
We can use tuple.toString() method to concatenate elements of tuple to string.
Example:
[php]val t = (1, “hello”,20.2356) println(“Concatenated String: ” + t.toString() )[/php]
Output
Concatenated String:(1, hello,20.2356)
Scala tuples can be converted to strings using various methods depending on the specific use case. Here are some examples:
- Using string interpolation:
scalaCopy codeval myTuple = ("hello", 123, true)
val myString = s"${myTuple._1} ${myTuple._2} ${myTuple._3}"
println(myString) // prints "hello 123 true"
In this example, we use string interpolation to create a string that contains the first, second, and third elements of the tuple separated by spaces.
- Using the
mkString()
method:
scalaCopy codeval myTuple = ("hello", 123, true)
val myString = myTuple.productIterator.mkString(", ")
println(myString) // prints "hello, 123, true"
In this example, we use the productIterator
method to iterate over the elements of the tuple and then use the mkString()
method to concatenate them into a single string separated by commas.
- Using the
toString()
method:
scalaCopy codeval myTuple = ("hello", 123, true)
val myString = myTuple.toString()
println(myString) // prints "(hello,123,true)"
In this example, we use the toString()
method to convert the tuple to a string. Note that this method returns a string that includes parentheses and commas to represent the structure of the tuple.
Depending on your specific use case, one of these methods might be more suitable than the others.