Scala Mutable 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:
import scala.collection.SortedMap
object GfG
{
def main(args : Array[String])
{
val m 1 = SortedMap( 3 - > "geeks" , 4 - > "for" , 4 - > "for" )
val result = m 1 .contains( 3 )
println(result)
}
}
|
Here, contains method has a key identical to the key present in the SortedMap stated above so, it returns true.
Example #2:
import scala.collection.SortedMap
object GfG
{
def main(args : Array[String])
{
val m 1 = SortedMap( 3 - > "geeks" , 4 - > "for" , 4 - > "for" )
val result = m 1 .contains( 5 )
println(result)
}
}
|
Here, contains method has a key which is not identical to the key present in the SortedMap stated above so, it returns false.
Last Updated :
04 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...