Tuples in Scala – How to Convert Scala Tuple to String

Free Scala course with real-time projects Start Now!!

In our last tutorial, we studied Scala Control Structure. In this tutorial, we will help you in understanding the concept of tuples in Scala programming language. You will learn to create scala tuples with examples and syntax. You will also learn iteration over the tuples in Scala, swapping of tuple elements and way to convert Scala tuple into String in scala function.

So, let’s start exploring Tuples in Scala.

Tuples in Scala - How to Convert Scala Tuple to String

Tuples in Scala – How to Convert Scala Tuple to String

What are the Tuples in Scala?

A tuple is a Scala collection which can hold multiple values of same or different type together so they can be passed around as a whole. Unlike an array or list, a tuple can hold objects with different types but they are also immutable.

Do you know What is Scala List?

Let us understand the same with an example of tuple holding an integer, a string, and a double:

val t = new Tuple3(1, "hello",20.2356)

A sshortcutfor the above code is :-

val t = (1, "hello",20.2356)

You can also create a tuple using the below syntax:

1 -> “a”

This syntax would be seen a lot when creating maps.

The actual type of a tuple depends upon the number of elements it contains and the type of those elements. Thus the type of (1,”hello”,20.2356) is Tuple3[Int , String , Double].Tuple are of type Tuple1 , Tuple2 ,Tuple3 and so on.

There is an upper limit of 22 for the element in the tuple in the scala, if you need more elements, then you can use a collection, not a tuple.

For each tuple type, Scala defines a number of element access methods.


[php]val t = (4,3,2,1)
val sum = t._1 + t._2 + t._3 + t._4
println( “Sum of elements: ” + sum )
// Sum of elements : 10[/php]

Iteration over the Tuple in Scala

You can use Tuple.productIterator() method to iterate over all of the elements of a tuple.


[php]val t = (4,3,2,1)
t.productIterator.foreach{ i => println(“Value = ” + i )} [/php]


Value = 4
Value = 3
Value = 2
Value = 1

Follow this link to know more about Scala Iterator

Swapping of Scala Tuple Elements

We can perform swapping of tuple elements by using tuple.swap method.


[php]val t = new Tuple2(“tuple”, “test”)
println(“Swapped Tuple: ” + t.swap )[/php]


Swapped tuple: (test,tuple)

Converting Tuple to String in Scala

We can use tuple.toString() method to concatenate elements of tuple to string.


[php]val t = (1, “hello”,20.2356)
println(“Concatenated String: ” + t.toString() )[/php]


Concatenated String:(1, hello,20.2356)

Read More about Scala String in detail


So, this was all about Tuples in Scala. Hope you like our explanation, share your feedback with us!

For further Scala knowledge, refer Best Scala books for beginners

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *