{"id":1633,"date":"2018-04-13T05:19:55","date_gmt":"2018-04-13T05:19:55","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=1633"},"modified":"2018-04-13T05:19:55","modified_gmt":"2018-04-13T05:19:55","slug":"scala-tuples","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/","title":{"rendered":"Scala Tuples with Scala Tuple type and Examples"},"content":{"rendered":"<h2>1. Scala Tuples<\/h2>\n<p>What are Scala Tuples? How do we iterate over them? How do we convert tuples in Scala to strings or swap their elements? These are the questions we will answer in this article.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/11\/Scala-Tuples-01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-73245 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2019\/11\/Scala-Tuples-01.jpg\" alt=\"scala tuples with scala tuple type and examples\" width=\"1200\" height=\"628\" \/><\/a><\/p>\n<h2>2. What are Scala Tuples?<\/h2>\n<p>Scala tuple holds together a fixed number of items so we can pass them around as a whole. Unlike a list or an array, we can put different types of objects in it; they are also immutable. Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">scala&gt; val t=(1,\"Ayushi\",Console)\nt: (Int, String, Console.type) = (1,Ayushi,scala.Console$@7210f559)\nNote that this is syntactic sugar for this:\nscala&gt; val t=new Tuple3(1,\"Ayushi\",Console)\nt: (Int, String, Console.type) = (1,Ayushi,scala.Console$@7210f559)\nYou can also declare a tuple the following way:\nscala&gt; 1-&gt;'a'\nres0: (Int, Char) = (1,a)\nscala&gt; 1-&gt;'a'-&gt;'b'\nres1: ((Int, Char), Char) = ((1,a),b)<\/pre>\n<p>We can also use Scala tuples to pass a list of data values as messages between actors in concurrent programming.<\/p>\n<h2>3. Scala Tuple Type and How to Access Them<\/h2>\n<p>The number of elements in a Scala tuple and their types decides a tuple\u2019s overall type. So, let\u2019s see some types. The type for (7,\u201dAyushi\u201d) is Tuple2(Int, String). That for (\u201cRed\u201d,\u201dGolden\u201d,\u2019o\u2019,7,\u2019g\u2019,4,\u201dAyushi\u201d) is Tuple6(String, String, Char, Int, Char, Int, String). This is how Scala decides the types of tuples- as Tuple1, Tuple2, Tuple3, and so on. 1&lt;=N&lt;=22. Scala will currently limit you to 22 members in a tuple, but you may use a collection instead if you want more. For a TupleN type, Scala gives us some methods to access the elements.<\/p>\n<p>Let\u2019s take the tuple t we defined earlier. To access the string \u201cAyushi\u201d from it, we type the following onto the console:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">scala&gt; t._2\nres0: String = Ayushi<\/pre>\n<p>Note that here, the indexing doesn\u2019t begin at 1 for once. If we try to access an index that doesn\u2019t exist in the tuple, it raises a console error:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">scala&gt; t._4\n&lt;console&gt;:13: error: value _4 is not a member of (Int, String, Console.type)\n      t._4\n        ^<\/pre>\n<h2>4. Scala Tuples Example<\/h2>\n<p>Before moving forward, let\u2019s take an example to demonstrate how we use tuples.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">object tuples extends App{\n    val t=(1,2,3)\n    val mult=t._1*t._2*t._3\n    println(\"These multiply to \"+mult)\n}<\/pre>\n<p>We save this as tuples.scala on our Desktop, then exit Scala and move to the Desktop on the command prompt.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">scala&gt; Terminate batch job (Y\/N)? y\nC:\\Users\\lifei&gt;cd Desktop\nC:\\Users\\lifei\\Desktop&gt;scalac tuples.scala\nC:\\Users\\lifei\\Desktop&gt;scala tuples<\/pre>\n<p>These multiply to 6<\/p>\n<p>As you can see, this prints the product of all three members of the tuple.<\/p>\n<h2>5. Iterating Over a Tuple in Scala<\/h2>\n<p>To iterate over an entire tuple, we use the method productIterator().<\/p>\n<p>object tuples extends App{<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">    val t=(1,2,3)\n    t.productIterator.foreach{i=&gt;println(i)}\n}\nAnd this is what we do in the command prompt:\nC:\\Users\\lifei\\Desktop&gt;scalac tuples.scala\nC:\\Users\\lifei\\Desktop&gt;scala tuples<\/pre>\n<p>1<\/p>\n<p>2<\/p>\n<p>3<\/p>\n<h2>6. Converting a Scala Tuple to String<\/h2>\n<p>Wait, what exactly does this mean? Well, converting a tuple to a string concatenates all of its elements into a string. We use the toString() method for this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">object tuples extends App{\n    val t=(\"Hello\",\" \",\"World\")\n    println(t.toString())\n}\nAnd in the command prompt:\nC:\\Users\\lifei\\Desktop&gt;scalac tuples.scala\nC:\\Users\\lifei\\Desktop&gt;scala tuples\n(Hello, ,World)<\/pre>\n<h2>7. Swapping Elements<\/h2>\n<p>Finally, let\u2019s discuss how we can swap elements in a tuple. This will allow us to exchange positions of elements in a Tuple2 type.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">object tuples extends App{\n    val t=(\"Hello\",\"World\")\n    println(t.swap)\n}\nThe command prompt shows:\nC:\\Users\\lifei\\Desktop&gt;scalac tuples.scala\nC:\\Users\\lifei\\Desktop&gt;scala tuples\n(World,Hello)<\/pre>\n<p>It swapped the two values to each other\u2019s positions.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>This is all about tuples in Scala. Let us know if anything confuses you. And remember to practice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Scala Tuples What are Scala Tuples? How do we iterate over them? How do we convert tuples in Scala to strings or swap their elements? These are the questions we will answer in&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":73245,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[880],"tags":[],"class_list":["post-1633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Tuples with Scala Tuple type and Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Scala Tuples: Learn What are Scala tuples, Types of Tuples in Scala, Iterating Over a Tuple, Converting a Tuple to a String, Swapping Elements etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Tuples with Scala Tuple type and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Scala Tuples: Learn What are Scala tuples, Types of Tuples in Scala, Iterating Over a Tuple, Converting a Tuple to a String, Swapping Elements etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-13T05:19:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Tuples with Scala Tuple type and Examples - TechVidvan","description":"Scala Tuples: Learn What are Scala tuples, Types of Tuples in Scala, Iterating Over a Tuple, Converting a Tuple to a String, Swapping Elements etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/","og_locale":"en_US","og_type":"article","og_title":"Scala Tuples with Scala Tuple type and Examples - TechVidvan","og_description":"Scala Tuples: Learn What are Scala tuples, Types of Tuples in Scala, Iterating Over a Tuple, Converting a Tuple to a String, Swapping Elements etc.","og_url":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2018-04-13T05:19:55+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Scala Tuples with Scala Tuple type and Examples","datePublished":"2018-04-13T05:19:55+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/"},"wordCount":437,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg","articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/","url":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/","name":"Scala Tuples with Scala Tuple type and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg","datePublished":"2018-04-13T05:19:55+00:00","description":"Scala Tuples: Learn What are Scala tuples, Types of Tuples in Scala, Iterating Over a Tuple, Converting a Tuple to a String, Swapping Elements etc.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/scala-tuples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2019\/11\/Scala-Tuples-01.jpg","width":1200,"height":628},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/scala-tuples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Scala Tuples with Scala Tuple type and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/1633\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/73245"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}