Tuples in Scala – How to Convert Scala Tuple to String

1. Objective

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

2. 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.

Examples:

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

3. Iteration over the Tuple in Scala

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

Example:

val t = (4,3,2,1)
t.productIterator.foreach{ i => println("Value = " + i )} 

Output

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

Follow this link to know more about Scala Iterator

Scala Quiz

4. Swapping of Scala Tuple Elements

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

Example

val t = new Tuple2("tuple", "test")
println("Swapped Tuple: " + t.swap )

Output

Swapped tuple: (test,tuple)

5. Converting Tuple to String in Scala

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

Example:

val t = (1, "hello",20.2356)
println("Concatenated String: " + t.toString() )

Output

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

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.