Scala map 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 Map interface of Scala. It checks whether the stated map contains a binding for a key or not.
Example :
object GfG
{
def main(args : Array[String])
{
val map : Map[Int,Int] = Map( 2 - > 3 )
val result = map.contains( 2 )
println(result)
}
}
|
Here, contains method has a key identical to the key present in the map stated above so, it returns true.
Example :
object GfG
{
def main(args : Array[String])
{
val map : Map[Int,Int] = Map( 4 - > 7 )
val result = map.contains( 5 )
println(result)
}
}
|
Here, contains method has a key which is not identical to the key present in the map stated above so, it returns false.
Last Updated :
28 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...