Open In App

Scala SortedSet intersect() method with example

Last Updated : 03 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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() 
// method 
import scala.collection.immutable.SortedSet 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a SortedSet 
        val s1 = SortedSet(1, 2, 3, 4, 5) 
        
        val s2 = SortedSet(11, 12, 13, 4, 5) 
        
        // Applying intersect method 
        val s3 = s1.intersect(s2) 
        
        s3.foreach(x => println(x))
    } 
} 
Output:
4
5
Example #2: Scala
// Scala program of intersect() 
// method 
import scala.collection.immutable.SortedSet 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a SortedSet 
        val s1 = SortedSet(1, 2, 3, 4, 5) 
        
        val s2 = SortedSet(11, 2, 3, 4, 5) 
        
        // Applying intersect method 
        val s3 = s1.intersect(s2) 
        
        s3.foreach(x => println(x))
    } 
} 
Output:
2
3
4
5

Next Article

Similar Reads