Scala Mutable SortedSet intersect() method

In Scala mutable collections, SortedSet 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of intersect() 
// method 
import scala.collection.mutable.SortedSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a SortedSet 
        val s1 = SortedSet(1, 12, 3, 14, 5
          
        val s2 = SortedSet(11, 2, 3, 14, 5
          
        // Applying intersect method 
        val s3 = s1.intersect(s2
          
        s3.foreach(x => println(x))
    

chevron_right


Output:

3
5
14

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of intersect() 
// method 
import scala.collection.mutable.SortedSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a SortedSet 
        val s1 = SortedSet(11, 22, 33, 44, 55
          
        val s2 = SortedSet(11, 2, 3, 4, 5
          
        // Applying intersect method 
        val s3 = s1.intersect(s2
          
        s3.foreach(x => println(x))
    

chevron_right


Output:

11



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.