Scala SortedMap contains() method with example

The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the SortedMap interface of Scala. It checks whether the stated SortedMap contains a binding for a key or not.

Method Definition: def contains(key: K): Boolean
Where, k is the key.

Return Type: It returns true if there is a binding for the key in the SortedMap stated else returns false.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of contains()
// method
import scala.collection.immutable.SortedMap
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a SortedMap
        val m1 = SortedMap(3 -> "geeks", 4 -> "for", 4 -> "for")
          
        // Applying contains method
        val result = m1.contains(3)
          
        // Displays output
        println(result)
  
    }
}                                         
                                        

chevron_right


Output:



true

Here, contains method has a key identical to the key present in the SortedMap stated above so, it returns true.
Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of contains()
// method
import scala.collection.immutable.SortedMap
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a SortedMap
        val m1 = SortedMap(3 -> "geeks", 4 -> "for", 4 -> "for")
          
        // Applying contains method
        val result = m1.contains(5)
          
        // Displays output
        println(result)
  
    }
}                                         
    

chevron_right


Output:

false

Here, contains method has a key which is not identical to the key present in the SortedMap stated above so, it returns false.




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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.