

{"id":13825,"date":"2018-04-18T08:41:30","date_gmt":"2018-04-18T08:41:30","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13825"},"modified":"2021-12-04T10:16:25","modified_gmt":"2021-12-04T04:46:25","slug":"scala-sets","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scala-sets\/","title":{"rendered":"Scala Sets | Learn About Sets in Scala Collections"},"content":{"rendered":"<p>In this Scala Sets tutorial, we will learn about sets, how to define Scala Sets, how to process them, and what methods to call on sets in Scala. We will also learn Declaring a set, Operations on sets in Scala collections, Concatenating Sets, Max and Min in a Scala set, and Finding Values common to two sets in Scala.<\/p>\n<p>So, let&#8217;s start the discussion on Scala Sets.<\/p>\n<h3>An Introduction to Sets in Scala<\/h3>\n<p>A Scala Set is a collection that won\u2019t take duplicates. By default, Scala uses immutable sets. But if you want, you can import the scala.collection.mutable.Set class. To be able to refer to both of these in the same collection, we can refer to the immutable set as Set and the mutable set as mutable.Set. You can also learn\u00a0<a href=\"https:\/\/data-flair.training\/blogs\/scala-array\/\">Scala Array<\/a> from our tutorial, once you are done with Scala sets.<\/p>\n<h3>Declaring a Set in Scala<\/h3>\n<p>You can either declare an empty set in Scala or one with values.<\/p>\n<h4>a. Declaring an Empty Set<\/h4>\n<p>We must include the type annotation when declaring an empty set, so it can be decided what concrete type to assign to a variable.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var s:Set[Int]=Set()\r\ns: Set[Int] = Set()<\/pre>\n<h4>b. Declaring a Set with Values<\/h4>\n<p>We may choose to elide the type annotation. When we do so, Scala will infer that from the type of values inside the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var s=Set(1,4,4,3)\r\ns: scala.collection.immutable.Set[Int] = Set(1, 4, 3)\r\nscala&gt; var s:Set[Int]=Set(1,4,4,3)\r\ns: Set[Int] = Set(1, 4, 3)<\/pre>\n<p>Note that this does not rearrange values as {1,3,4} as Python would do.<\/p>\n<h3>Operations on Scala Sets<\/h3>\n<p>Scala has the following three operations on a set:<\/p>\n<h4>a. head<\/h4>\n<p>This will return the first element of a Scala set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.head\r\nres0: Int = 1<\/pre>\n<h4>b. tail<\/h4>\n<p>This will return all elements of a set except the first.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.tail\r\nres1: scala.collection.immutable.Set[Int] = Set(4, 3)<\/pre>\n<h4>c. isEmpty<\/h4>\n<p>If the set is empty, this will return a Boolean true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.isEmpty\r\nres2: Boolean = false<\/pre>\n<h3>Concatenating Scala Sets<\/h3>\n<p>There are two ways to concatenate sets. Let\u2019s take another Scala set for this:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var s1=Set(7,9,8,9)\r\ns1: scala.collection.immutable.Set[Int] = Set(7, 9, 8)<\/pre>\n<h4>a. The ++ Operator<\/h4>\n<pre class=\"EnlighterJSRAW\">scala&gt; s++s1\r\nres3: scala.collection.immutable.Set[Int] = Set(1, 9, 7, 3, 8, 4)\r\nscala&gt; s++s1\r\nres4: scala.collection.immutable.Set[Int] = Set(1, 9, 7, 3, 8, 4)\r\nscala&gt; s1++s\r\nres5: scala.collection.immutable.Set[Int] = Set(1, 9, 7, 3, 8, 4)<\/pre>\n<h4>b. The Set.++() Method<\/h4>\n<p>We can call the ++() method to either set and pass the other to it.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.++(s1)\r\nres6: scala.collection.immutable.Set[Int] = Set(1, 9, 7, 3, 8, 4)\r\nscala&gt; s1.++(s)\r\nres7: scala.collection.immutable.Set[Int] = Set(1, 9, 7, 3, 8, 4)<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-operator\/\">Scala Operators with Syntax and Examples <\/a><\/strong><\/p>\n<h3>Max and Min in a Set<\/h3>\n<p>We can find the maximum and minimum values in a set.<\/p>\n<h4>a. max<\/h4>\n<p>This returns the maximum value from a set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.max\r\nres8: Int = 4<\/pre>\n<h4>b. min<\/h4>\n<p>This returns the minimum value from a set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; s.min\r\nres9: Int = 1<\/pre>\n<h3>Finding Values Common to Two Sets<\/h3>\n<p>To determine the values common to two certain sets, we can use one of two methods. Let\u2019s take two new sets for this:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var a=Set(1,4,3,4,2)\r\na: scala.collection.immutable.Set[Int] = Set(1, 4, 3, 2)\r\nscala&gt; var b=Set(7,9,8,2,8)\r\nb: scala.collection.immutable.Set[Int] = Set(7, 9, 8, 2)<\/pre>\n<h4>a. The Set.&amp;() Method<\/h4>\n<p>We can call &amp;() on one set, and pass another to it.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.&amp;(b)\r\nres10: scala.collection.immutable.Set[Int] = Set(2)<\/pre>\n<h4>b. Set.intersect() Method<\/h4>\n<p>We can use the intersect() method.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.intersect(b)\r\nres11: scala.collection.immutable.Set[Int] = Set(2)\r\nscala&gt; b.intersect(a)\r\nres12: scala.collection.immutable.Set[Int] = Set(2)<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-functions\/\">Scala Functions with Syntax and Examples <\/a><\/strong><\/p>\n<h3>Methods to Call on a Set<\/h3>\n<p>Playing around with sets in Scala, you can call these methods on them. (Note that they do not modify the set itself)<\/p>\n<h4>a. def +(elem: A): Set[A]<\/h4>\n<p>This adds an element to the set and returns it. (But doesn\u2019t modify the original set)<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.+(\"6\")\r\nres15: String = Set(1, 4, 3, 2)6<\/pre>\n<h4>b. def -(elem: A): Set[A]<\/h4>\n<p>This removes the element from the set and then returns it. Note that this takes an Int instead of a string for our set of Ints.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.-(2)\r\nres19: scala.collection.immutable.Set[Int] = Set(1, 4, 3)\r\nscala&gt; a.-(6)\r\nres21: scala.collection.immutable.Set[Int] = Set(1, 4, 3, 2)<\/pre>\n<h4>c. def contains(elem: A): Boolean<\/h4>\n<p>If the set contains that element, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.contains(2)\r\nres22: Boolean = true\r\nscala&gt; a.contains(6)\r\nres23: Boolean = false<\/pre>\n<h4>d. def &amp;(that: Set[A]): Set[A]<\/h4>\n<p>This returns an intersection of two sets, as we just saw.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.&amp;(b)\r\nres24: scala.collection.immutable.Set[Int] = Set(2)<\/pre>\n<h4>e. def &amp;~(that: Set[A]): Set[A]<\/h4>\n<p>&amp;~ stands for set difference.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.&amp;~(b) #All elements that are in a, but not in b\r\nres25: scala.collection.immutable.Set[Int] = Set(1, 4, 3)\r\nscala&gt; b.&amp;~(a) #All elements that are in b, but not in a\r\nres26: scala.collection.immutable.Set[Int] = Set(7, 9, 8)<\/pre>\n<h4>f. def +(elem1: A, elem2: A, elems: A*): Set[A]<\/h4>\n<p>This adds multiple elements to a Scala set and returns it.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.+(0,6,7)\r\nres27: scala.collection.immutable.Set[Int] = Set(0, 1, 6, 2, 7, 3, 4)<\/pre>\n<h4>g. def ++(elems: A): Set[A]<\/h4>\n<p>This concatenates a set with another collection.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.++(List(7,9,8))\r\nres29: scala.collection.immutable.Set[Int] = Set(1, 9, 2, 7, 3, 8, 4)\r\nscala&gt; a.++(b)\r\nres30: scala.collection.immutable.Set[Int] = Set(1, 9, 2, 7, 3, 8, 4)<\/pre>\n<h4>h. def -(elem1: A, elem2: A, elems: A*): Set[A]<\/h4>\n<p>This removes, each element mentioned from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.-(2,3,6)\r\nres31: scala.collection.immutable.Set[Int] = Set(1, 4)<\/pre>\n<h4>i. def addString(b: StringBuilder): StringBuilder<\/h4>\n<p>This adds all elements of the set to the String Builder.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.addString(new StringBuilder())\r\nres32: StringBuilder = 1432<\/pre>\n<h4>j. def addString(b: StringBuilder, sep: String): StringBuilder<\/h4>\n<p>This uses a separator to the above functionality.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.addString(new StringBuilder(),\"*\")\r\nres33: StringBuilder = 1*4*3*2<\/pre>\n<h4>k. def apply(elem: A)<\/h4>\n<p>This checks whether the element is part of the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.apply(1)\r\nres34: Boolean = true\r\nscala&gt; a.apply(7)\r\nres35: Boolean = false<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-data-types\/\">Scala DataTypes <\/a><\/strong><\/p>\n<h4>l. def count(p: (A) =&gt; Boolean): Int<\/h4>\n<p>This returns the count of elements that satisfy the predicate.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.count(x=&gt;{x%2!=0})\r\nres36: Int = 2<\/pre>\n<h4>m. def copyToArray(xs: Array[A], start: Int, len: Int): Unit<\/h4>\n<p>These copies len elements<i><\/i> from the set to the Array <i>xs<\/i>, starting at position <i>start<\/i>.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var c=Array(0,0,0,0,0,0,0,0)\r\nc: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0)\r\nscala&gt; a.copyToArray(c,3,2)\r\nscala&gt; c\r\nres39: Array[Int] = Array(0, 0, 0, 1, 4, 0, 0, 0)<\/pre>\n<h4>n. def diff(that: Set[A]): Set[A]<\/h4>\n<p>This returns the set difference(elements existing in one set, but not in another)<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.diff(b)\r\nres40: scala.collection.immutable.Set[Int] = Set(1, 4, 3)\r\nscala&gt; b.diff(a)\r\nres42: scala.collection.immutable.Set[Int] = Set(7, 9, 8)<\/pre>\n<h4>o. def drop(n: Int): Set[A]]<\/h4>\n<p>This returns all elements except the first n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.drop(2)\r\nres45: scala.collection.immutable.Set[Int] = Set(3, 2)<\/pre>\n<h4>p. def dropRight(n: Int): Set[A]<\/h4>\n<p>This Scala set returns all elements except the last n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.dropRight(2)\r\nres46: scala.collection.immutable.Set[Int] = Set(1, 4)<\/pre>\n<h4>q. def dropWhile(p: (A) =&gt; Boolean): Set[A]<\/h4>\n<p>This drops elements until the first element that doesn\u2019t satisfy the predicate.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.dropWhile(x=&gt;{x%2!=0})\r\nres48: scala.collection.immutable.Set[Int] = Set(4, 3, 2)<\/pre>\n<h4>r. def equals(that: Any): Boolean<\/h4>\n<p>This set in Scala compares the set to another sequence.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.equals(List(1,4,3,2))\r\nres50: Boolean = false<\/pre>\n<h4>s. def exists(p: (A) =&gt; Boolean): Boolean<\/h4>\n<p>If the predicate holds true for some elements in the set, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.exists(x=&gt;{x%4==0})\r\nres52: Boolean = true\r\nscala&gt; a.exists(x=&gt;{x%5==0})\r\nres53: Boolean = false<\/pre>\n<h4>t. def filter(p: (A) =&gt; Boolean): Set[A]<\/h4>\n<p>This filters such elements (see the previous method)<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.filter(x=&gt;{x%4==0})\r\nres54: scala.collection.immutable.Set[Int] = Set(4)<\/pre>\n<h4>u. def find(p: (A) =&gt; Boolean): Option[A]<\/h4>\n<p>This Scala set returns the first element that satisfies the predicate.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.find(x=&gt;{x%2==0})\r\nres55: Option[Int] = Some(4)<\/pre>\n<h4>v. def forall(p: (A) =&gt; Boolean): Boolean<\/h4>\n<p>This returns true if all elements of the set satisfy the predicate; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.forall(x=&gt;{x%2==0})\r\nres56: Boolean = false<\/pre>\n<h4>w. def head: A<\/h4>\n<p>This Scala Set returns the first element from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.head\r\nres59: Int = 1<\/pre>\n<h4>x. def init: Set[A]<\/h4>\n<p>This returns all elements from the set, except the last.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.init\r\nres62: scala.collection.immutable.Set[Int] = Set(1, 4, 3)<\/pre>\n<h4>y. def intersect(that: Set[A]): Set[A]<\/h4>\n<p>This returns the intersection of two sets(elements common to both).<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.intersect(b)\r\nres64: scala.collection.immutable.Set[Int] = Set(2)\r\nscala&gt; b.intersect(a)\r\nres65: scala.collection.immutable.Set[Int] = Set(2)<\/pre>\n<h4>z. def isEmpty: Boolean<\/h4>\n<p>This set in Scala returns true if the set is empty; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.isEmpty\r\nres66: Boolean = false<\/pre>\n<p><strong>Read: <a href=\"https:\/\/data-flair.training\/blogs\/scala-string\/\">Scala String: Creating String, Concatenation, String Length<\/a><\/strong><\/p>\n<h4>aa. def iterator: Iterator[A]<\/h4>\n<p>This creates a new iterator over the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.iterator\r\nres67: Iterator[Int] = non-empty iterator<\/pre>\n<h4>ab. def last: A<\/h4>\n<p>This returns the last element from a set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.last\r\nres68: Int = 2<\/pre>\n<h4>ac. def map[B](f: (A) =&gt; B): immutable.Set[B]<\/h4>\n<p>This Scala Set applies the function to all elements of the set and returns it.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.map(x=&gt;x*2)\r\nres69: scala.collection.immutable.Set[Int] = Set(2, 8, 6, 4)<\/pre>\n<h4>ad.<strong> def max: A<\/strong><\/h4>\n<p>This returns the highest value from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.max\r\nres70: Int = 4<\/pre>\n<h4>ae. def min: A<\/h4>\n<p>This returns the lowest element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.min\r\nres71: Int = 1<\/pre>\n<h4>af. def mkString: String<\/h4>\n<p>This Scala set represents all elements of the set as a String.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.mkString\r\nres72: String = 1432<\/pre>\n<h4>ag. def mkString(sep: String): String<\/h4>\n<p>This lets us define a separator for the above method\u2019s functionality.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.mkString(\"*\")\r\nres73: String = 1*4*3*2<\/pre>\n<h4>ah. def product: A<\/h4>\n<p>This returns the algebraic product of all elements in the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.product\r\nres74: Int = 24<\/pre>\n<h4>ai. def size: Int<\/h4>\n<p>This returns the size of the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.size\r\nres75: Int = 4<\/pre>\n<h4>aj. def splitAt(n: Int): (Set[A], Set[A])<\/h4>\n<p>This splits the set at the given index and returns the two resulting subsets.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.splitAt(2)\r\nres76: (scala.collection.immutable.Set[Int], scala.collection.immutable.Set[Int]) = (Set(1, 4),Set(3, 2))\r\nscala&gt; a.splitAt(3)\r\nres77: (scala.collection.immutable.Set[Int], scala.collection.immutable.Set[Int]) = (Set(1, 4, 3),Set(2))<\/pre>\n<h4>ak. def subsetOf(that: Set[A]): Boolean<\/h4>\n<p>If the set passed as argument is a subset of this set, this returns true; else, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; Set(3,2).subsetOf(a)\r\nres79: Boolean = true\r\nscala&gt; Set(2,3).subsetOf(a)\r\nres80: Boolean = true\r\nscala&gt; Set(1,3,4).subsetOf(a)\r\nres82: Boolean = true<\/pre>\n<h4>al. def sum: A<\/h4>\n<p>This returns the sum of all elements of the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.sum\r\nres83: Int = 10<\/pre>\n<h4>am. def tail: Set[A]<\/h4>\n<p>This returns all elements of the set except the first.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.tail\r\nres84: scala.collection.immutable.Set[Int] = Set(4, 3, 2)<\/pre>\n<h4>an. def take(n: Int): Set[A]<\/h4>\n<p>This Scala set returns the first n elements from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.take(3)\r\nres85: scala.collection.immutable.Set[Int] = Set(1, 4, 3)<\/pre>\n<h4>ao. def takeRight(n: Int):Set[A]<\/h4>\n<p>This returns the last n elements.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.takeRight(3)\r\nres86: scala.collection.immutable.Set[Int] = Set(4, 3, 2)<\/pre>\n<h4>ap. def toArray: Array[A]<\/h4>\n<p>This returns an Array holding elements from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toArray\r\nres87: Array[Int] = Array(1, 4, 3, 2)<\/pre>\n<h4>aq. def toList: List[A]<\/h4>\n<p>This returns a List from elements of the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toList\r\nres88: List[Int] = List(1, 4, 3, 2)<\/pre>\n<h4>ar. def toSeq: Seq[A]<\/h4>\n<p>This returns a sequence from the set.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toSeq\r\nres90: Seq[Int] = Vector(1, 4, 3, 2)<\/pre>\n<h4>as. def toString(): String<\/h4>\n<p>This represents the elements of the set as a String.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; a.toString\r\nres91: String = Set(1, 4, 3, 2)<\/pre>\n<p>This was all about Scala Sets.<\/p>\n<h3>Conclusion<\/h3>\n<p>This is all about how to declare and process Scala sets. Next, we will discuss Scala Maps. Furthermore, if you have any query, feel free to ask in the comment section.<br \/>\n<a href=\"http:\/\/otfried.org\/scala\/set.html\">For reference<\/a><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1975,&quot;href&quot;:&quot;http:\\\/\\\/otfried.org\\\/scala\\\/set.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250119133730\\\/https:\\\/\\\/otfried.org\\\/scala\\\/set.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 14:47:12&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 07:58:28&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 19:03:58&quot;,&quot;http_code&quot;:503},{&quot;date&quot;:&quot;2026-05-29 13:10:07&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-06 07:40:34&quot;,&quot;http_code&quot;:503}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-06 07:40:34&quot;,&quot;http_code&quot;:503},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Scala Sets tutorial, we will learn about sets, how to define Scala Sets, how to process them, and what methods to call on sets in Scala. We will also learn Declaring a&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":34727,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[2862,3639,8606,8695,9282,12545],"class_list":["post-13825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-concatenating-scala-sets","tag-declaring-a-set","tag-max-and-min-in-a-set","tag-methods-to-call-on-a-set","tag-operations-on-sets","tag-scala-sets"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scala Sets | Learn About Sets in Scala Collections - DataFlair<\/title>\n<meta name=\"description\" content=\"Scala Sets:Learn Declaring a set,operations on sets in Scala collections,concatenating Sets, Max-Min in a Scala set,Find Values to compare two sets in Scala\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scala-sets\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Sets | Learn About Sets in Scala Collections - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Scala Sets:Learn Declaring a set,operations on sets in Scala collections,concatenating Sets, Max-Min in a Scala set,Find Values to compare two sets in Scala\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scala-sets\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-18T08:41:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:46:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.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=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Sets | Learn About Sets in Scala Collections - DataFlair","description":"Scala Sets:Learn Declaring a set,operations on sets in Scala collections,concatenating Sets, Max-Min in a Scala set,Find Values to compare two sets in Scala","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:\/\/data-flair.training\/blogs\/scala-sets\/","og_locale":"en_US","og_type":"article","og_title":"Scala Sets | Learn About Sets in Scala Collections - DataFlair","og_description":"Scala Sets:Learn Declaring a set,operations on sets in Scala collections,concatenating Sets, Max-Min in a Scala set,Find Values to compare two sets in Scala","og_url":"https:\/\/data-flair.training\/blogs\/scala-sets\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-18T08:41:30+00:00","article_modified_time":"2021-12-04T04:46:25+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Scala Sets | Learn About Sets in Scala Collections","datePublished":"2018-04-18T08:41:30+00:00","dateModified":"2021-12-04T04:46:25+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/"},"wordCount":1308,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.jpg","keywords":["Concatenating Scala Sets","Declaring a Set","Max and Min in a Set","Methods to Call on a Set","Operations on Sets","Scala Sets"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scala-sets\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/","url":"https:\/\/data-flair.training\/blogs\/scala-sets\/","name":"Scala Sets | Learn About Sets in Scala Collections - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.jpg","datePublished":"2018-04-18T08:41:30+00:00","dateModified":"2021-12-04T04:46:25+00:00","description":"Scala Sets:Learn Declaring a set,operations on sets in Scala collections,concatenating Sets, Max-Min in a Scala set,Find Values to compare two sets in Scala","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scala-sets\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Sets-01-1.jpg","width":1200,"height":628,"caption":"Scala Sets | Learn About Sets in Scala Collections"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scala-sets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Scala Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/scala\/"},{"@type":"ListItem","position":3,"name":"Scala Sets | Learn About Sets in Scala Collections"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13825"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13825\/revisions"}],"predecessor-version":[{"id":104783,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13825\/revisions\/104783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/34727"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}