Scala Set intersect() method with example Last Updated : 18 Oct, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The intersect() method is utilized to compute the intersection between two sets. Method Definition: def intersect(that: Set[A]): Set[A] Return Type: It returns a set containing the elements present in both the sets. Example #1: Scala // Scala program of intersect() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a set val s1 = Set(1, 2, 3, 4, 5) val s2 = Set(11, 12, 13, 4, 5) // Applying intersect method val s3 = s1.intersect(s2) s3.foreach(x => println(x)) } } Output: 5 4 Example #2: Scala // Scala program of intersect() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a set val s1 = Set(1, 2, 3, 4, 5) val s2 = Set(11, 2, 3, 4, 5) // Applying intersect method val s3 = s1.intersect(s2) s3.foreach(x => println(x)) } } Output: 5 2 3 4 Comment More infoAdvertise with us Next Article Scala Stack intersect() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Scala-Set +1 More Similar Reads Scala TreeSet intersect() method with example In Scala TreeSet class, the intersect() method is utilized to return a new TreeSet that consists of elements that are present in both the given TreeSet. Method Definition: def intersect[B >: A](that: collection.Seq[B]): TreeSet[A] Return Type: It returns a new TreeSet that consists of elements th 2 min read Scala Stack intersect() method with example In Scala Stack class, the intersect() method is utilized to return a new stack that consists of elements that are present in both the given stacks. Method Definition: def intersect[B >: A](that: collection.Seq[B]): Stack[A] Return Type: It returns a new stack that consists of elements that are pr 2 min read Scala SortedSet intersect() method with example The intersect() method is utilized to compute the intersection between two SortedSets. Method Definition: def intersect(that: SortedSet[A]): SortedSet[A] Return Type: It returns a SortedSet containing the elements present in both the SortedSets. Example #1: Scala // Scala program of intersect() // m 1 min read Scala Set filter() method with example The filter() method is utilized to select all elements of the set which satisfies a stated predicate. Method Definition: def filter(p: (A) => Boolean): Set[A] Return Type: It returns a set containing all the elements of the set which satisfies the given predicate. Example #1: Scala // Scala progr 1 min read Scala Set init() method with example The init() method is utilized to find all the elements of the set except the last one. Method Definition: def init: Set[A] Return Type: It returns all the elements of the set except the last one. Example #1: Scala // Scala program of init() // method // Creating object object GfG { // Main method de 1 min read Scala Queue intersect() method with example The intersect() method is utilized to return a new queue that consists of elements that are present in both the given queues. Method Definition: def intersect[B >: A](that: collection.Seq[B]): Queue[A] Return Type: It returns a new queue that consists of elements that are present in both the give 2 min read Like