Scala tuple11/12/2022 n, you have to type in tuple._n, where the tuple is the name of a tuple and n th element is fetched.įor example, if we need fetch 6 th element of the tuple t then we will type t._6 to access it. The access method for accessing an element of a tuple is quite easy and convenient. There is one more method to define a tuple by using the -> operator: Val tuple_name = new tuple 2(data 1, data 2) You can define a tuple by mentioning its tuple class, the class is simply tuples, where n is the number of elements the tuple has. This is a shortcut way of defining a tuple. You can easily use tuples in your scala program for usage. The maximum number of miscellaneous elements that a tuple can have is twenty-two. The Scala tuples are immutable, i.e the objects of different type can be stored in the tuple but the value of these objects cannot be changed. The elements of Scala tuples may or may not be the same data type. Submitted by Shivang Yadav, on July 20, 2019Ī tuple is a data structure that has multiple elements. Scala programming also supports tuples and in this Scala tutorials on tuples, we will learn about tuples with examples. This may be preferable instead of using a case class, particularly if we need to return unrelated values.Scala Tuples: Tuple is a collection of similar or different multiple elements. The best place where we can use tuple is, when we want to return multiple values from a method or pass multiple values to a function. Because they aren’t a collection, they don’t have methods like map, filter, etc. Note: Technically, Scala 2.x tuples aren’t collections classes, they’re just a convenient little container. We can use tuple.swap Method for Swapping the elements of a tuple. The foreach method takes function as a parameter and applies it to every element in the tuple, names. We can use productIterator() method to iterate over all the elements of a Tuple. Rock has wrong relation with Alice Iterating over a Scala tuple List(("Bob", "Father"), ("Jack", "Brother"), ("Rock", "Friend"))Ĭase _ => println(relation(2)._1 + " has wrong relation with Alice") Pattern matching is a mechanism for checking a value against a pattern.Īnother Example for pattern-matching a tuple: val relation = It is a great way to assign tuple elements to variables. The another approach to access elements of a tuple is pattern-matching. One approach is to access them by element number, where the number is preceded by an underscore, method tuple._i is used to access the ith element of the tuple. Operations on Scala Tuple Access element from tuple Currently there is an upper limit of 22 in the Scala and if we need more, then we will have to use a collection, not a tuple. Tuples can be of type Tuple1, Tuple2, Tuple3 and so on. Thus, the type of (1, “hello world”) is Tuple2. The type of a tuple depends upon the number of elements it contains and the types of those elements. Which is a syntactic sugar for the following- val t = new Tuple2(1, "hello world") An example of a tuple storing an integer, and a string value. Click here to know more about mutability and immutability. A tuples purpose is to combine a fixed and finite number of items together to allow the programmer to pass a tuple as a whole. In Scala, a tuple is a class that gives us a simple way to store heterogeneous items or different data types in the same container.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |