{"id":5698,"date":"2020-10-08T04:58:42","date_gmt":"2020-10-08T04:58:42","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=5698"},"modified":"2024-04-12T06:05:17","modified_gmt":"2024-04-12T06:05:17","slug":"sets-in-about-sets-in-scala-collections","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/","title":{"rendered":"Scala &#8211; Set"},"content":{"rendered":"\r\n<h2 class=\"wp-block-heading\"><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Learn About Sets in Scala Collections&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Learn About Sets in Scala Collections<\/span><\/h2>\r\n<h3>\u00a0<\/h3>\r\n<h3>Set:<\/h3>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">They are\u00a0<code><code><\/code><\/code><strong>Iterables<\/strong> that contain no duplicate elements. The operations on sets are summarized in the following table for general sets and in the table after that for mutable sets.<\/p>\r\n\r\n\r\n\r\n<p>Sets are a fundamental data structure in Scala collections that represent a collection of unique elements with no duplicate values. In Scala, sets are immutable by default, meaning that once a set is created, its elements cannot be changed. Sets are implemented using hash tables, which provide efficient lookup, insertion, and deletion operations.<\/p>\r\n<p>To create a set in Scala, you can use the <code>Set<\/code> object and specify the elements:<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 By default immutable<\/p>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">\u00a0 They do not maintain order<\/p>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">\u00b7\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Set can\u2019t be access by their Index.<\/p>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">\u00a0No Duplicity and Intersection &amp; Union is simple<\/p>\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">1. Example program to show usage of the basic set operational methods:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">package Collection\r\n\r\nobject Set1 {\r\n\r\ndefmain(args: Array[String]) {\r\n\r\nvallang1 = <\/pre>\r\n<p><em>1Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">(\"Python\", \"DataScience\", \"Sql\")\r\n\r\nvalnums: Set[Int] = <\/pre>\r\n<p><em>Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">()\r\n\r\n<\/pre>\r\n<p><em>1println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Best of Programming Language : \" + lang1.head )\r\n\r\n<\/pre>\r\n<p><em>println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Other Programming Language : \" + lang1.tail )\r\n\r\n<\/pre>\r\n<p><em>2println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Check if Set is empty : \" + lang1.isEmpty )\r\n\r\n<\/pre>\r\n<p><em>println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Check if nums is empty : \" + nums.isEmpty )\r\n\r\n\u00a0 }\r\n\r\n}<\/pre>\r\n\r\n\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">2. Example program to show usage of the set with respect to concatenation:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">package Collection\r\n\r\nobject Set2 {\r\n\r\ndefmain(args: Array[String]) {\r\n\r\nvallang1 = <\/pre>\r\n<p><em>1Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">(\"Python\", \"DataScience\", \"Sql\")\r\n\r\nvallang2 = <\/pre>\r\n<p><em>Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">(\"Hadoop\", \"Scala\",\"AWS\")\r\n\r\n\/\/ using two sets with ++ as operator\r\n\r\nvarlang = lang1 ++ lang2\r\n\r\n<\/pre>\r\n<p><em>1println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"lang1 ++ lang2 : \" + lang )\r\n\r\n\/\/ using two sets with ++ as method\r\n\r\nlang = lang1.++(lang2)\r\n\r\n<\/pre>\r\n<p><em>println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"lang1.++(lang2) : \" + lang )\r\n\u00a0 }\r\n}\r\n<\/pre>\r\n\r\n\r\n\r\n\r\n\r\n<p class=\"has-medium-font-size\">3. Example program to show usage of the set to find <a href=\"https:\/\/prwatech.in\/blog\/#\">minimum<\/a> and\u00a0maximum of the elements <a href=\"https:\/\/www.tableau.com\/\">available<\/a> in a set and find common elements between two sets :<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\">package Collection\r\n\r\nobject Set3 {\r\n\r\ndefmain(args: Array[String]) {\r\n\r\nvalnum1 = <\/pre>\r\n<p><em>Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">(1,6,13,21,32,47)\r\n\r\nvalnum2 = <\/pre>\r\n<p><em>#Set<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">(21,62,9,32,38,57)\r\n\r\n\/\/ min and max of the elements\r\n\r\n<\/pre>\r\n<p><em>2println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Min element in Set(1,6,13,21,32,47) : \" + num1.min )\r\n\r\n<\/pre>\r\n<p><em>#println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"Max element in Set(1,6,13,21,32,47) : \" + num1.max )\r\n\r\n\/\/ to find common elements between two sets\r\n\r\n<\/pre>\r\n<p><em>eprintln<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"num1.&amp;(num2) : \" + num1.&amp;(num2) )\r\n\r\n<\/pre>\r\n<p><em>#println<\/em><\/p>\r\n<pre class=\"wp-block-preformatted\">( \"num1.intersect(num2) : \" + num1.intersect(num2) )\r\n\r\n\u00a0 }\r\n}<\/pre>\r\n\r\n\r\n\r\n\r\n\r\n<p>Learn About Sets in Scala Collections<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Learn About Sets in Scala Collections \u00a0 Set: They are\u00a0Iterables that contain no duplicate elements. The operations on sets are summarized in the following table for general sets and in the table after that for mutable sets. Sets are a fundamental data structure in Scala collections that represent a collection of unique elements with no [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1986,1984,1985,1983],"class_list":["post-5698","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-guide-to-to-scala-collections","tag-scala-standard-library-2-12-8-scala-collection-set","tag-set-in-scala-set-1","tag-sets-collections-scala-2-8-2-12"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Learn About Sets in Scala Collections - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn About Sets in Scala Collections - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-08T04:58:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T06:05:17+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/\",\"name\":\"Learn About Sets in Scala Collections - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2020-10-08T04:58:42+00:00\",\"dateModified\":\"2024-04-12T06:05:17+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Set\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn About Sets in Scala Collections - Prwatech","description":"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Learn About Sets in Scala Collections - Prwatech","og_description":"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2020-10-08T04:58:42+00:00","article_modified_time":"2024-04-12T06:05:17+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/","name":"Learn About Sets in Scala Collections - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2020-10-08T04:58:42+00:00","dateModified":"2024-04-12T06:05:17+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Learn About Sets in Scala Collections - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/sets-in-about-sets-in-scala-collections\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Set"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=5698"}],"version-history":[{"count":8,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5698\/revisions"}],"predecessor-version":[{"id":11358,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/5698\/revisions\/11358"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=5698"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=5698"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=5698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}