Introduction to Scala Tuples
A tuple is a data structure which can store elements of the different data type. It is also used for storing and retrieving of data. In scala, tuples are immutable in nature and store heterogeneous types of data. 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...)
Now we can see one practice syntax to see how to define it with different data type available in scala;
var myTuple = (Int, String, String, Int)
var myTuple1 = (001, "Amit", "Mumbai", 24)
In this way, we can define our tuple in scala. We have created one tuple with data type int and String and in the next line, we are trying to provide some values into the tuple.
How Does Tuples Work in Scala?
In scala, tuples can store a fixed number of elements into it. The maximum limit for storing elements is 22. If we try to store elements greater its size then it will generate one error. But in scala, we have one alternative to overcome this problem and that is nested tuples by which we can create tuples inside tuples.
In scala we have types of tuple which are as follows:
- Tuple1
- Tuple2
- Tuple3
- Tuple4
- Tuple5 and so on.
For instance, this number is 22. If we want to store more elements than we should go for collection in scala. This can be useful where small data is available. We can access the elements of the tuple by using their index or position we can say for that we need to us ._ syntax.
4.5 (5,543 ratings)
View Course
Now the question comes where we can use tuples? So if we want to group some elements which are of different type then we can go for a tuple in scala. They maintain the order as well and part of the collection.
Try to create on tuple;
val myTuple1: (Int, String) = (2, "Hello")
Here we are creating one tuple but internally it will use one of the class of tuple i.e. from tuple1 to tuple22 it’s an internal process. Tuples can be helpful when we want to return the multiple values from our method. So we can say that the scala tuples are the ordered collection and elements inside tuples either related to each other or not both can possible.
Things to Remember
Some point we should keep in mind while working with tuples in scala which are as follows;
1. Suppose we have one requirement where our data inside the collection is not changing frequently or we can say it once assigned and changes after a long period of time then in this case we should go for tuples.
2. Tuples are very fast in operation and it is considered as the fastest data structure. Also, it contains only limited elements in it so the memory consumption is very less.
3. One advantage of using tuples is that it can store elements of different data types that means it is heterogeneous in nature. But if we compare it with List then in List collection we can only store data of the same type.
4. Here tuple in scala is immutable.
5. Tuples can easily replace arrays because it is more efficient than all compare areas. They have similarities they are fixed in nature.
Examples to Implement Scala Tuples
Below are the Example of Scala Tuples:
Example #1
In this example, we will create one simple tuple and print the output.
Code:
object Main extends App{
// Your code here!
// creating tuple object
val t = new Tuple1(1, 2, 3, "hello", "bye", "wait", 00.3)
// printing out the values
println("values inside the tuples are :: ")
println(t)
}
Output:
Explanation: Here in the above example we are creating one main method inside this main method we are creating a Tuple1 object and assigning it values. To create a tuple object we have just used the new keyword here to create the object. After the creation of the object, we can assign them values.
Example #2
We can also use the toString() method here to convert a tuple to String. This method will concatenate all of the data available in the tuple to string.
In this example, we are showing its use with a tuple in scala.
Code:
object Main extends App{
// Your code here!
// creating tuple object
val t = new Tuple1(1, 2, 3, "hello", "bye", "wait", 00.3)
// printing out the values
println("values inside the tuples are :: ")
println(t)
//print by using toString()
println("Using toString() meths here ::")
println(t.toString)
}
Output:
Example #3
In this example, we will see another way of defining tuple in scala and accessing its elements by the variable name.
Code:
object Main extends App{
// Your code here!
// creating tuple object
var (t1, t2, t3, t4, t5)= (100, "Amit", "Sharma", 30, "Mumbai")
// printing out the values
println("values inside the tuples are :: ")
//print by using toString()
println("Using variable names here ::")
println(t1)
println(t2)
println(t3)
println(t4)
println(t5)
}
Output:
Example #4
In this example, we are iterating our tuples elements. We can perform any operation on this inside the iterator. For iterating the elements we are using product Iterator available in scala.
Code:
object Main extends App{
// Your code here!
// creating tuple object
var myTuple = (100, "Amit", "Sharma", 30, "Mumbai", 1)
// printing out the values
println("values inside the tuples are :: ")
println(myTuple)
//print by iterating elements of the tuples::
println("Using matching meths here ::")
myTuple.productIterator.foreach{x=>println(x)}
}
Output:
Conclusion
In scala tuples, we can store heterogeneous elements. These elements are of a different types. But they have their limits for element string in scala we can store up to 22 element maximum. We can use nested tuples instead.
Recommended Articles
This is a guide to Scala Tuples. Here we discuss a brief overview on Scala Tuples and its different examples along with code Implementation. You can also go through our other suggested articles to learn more –