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.
-
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 map stated else returns false.
Example :
// Scala program of contains() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val map: Map[Int,Int] = Map(2 -> 3) // Applying contains method val result = map.contains(2) // Displays output println(result) } } |
true
Here, contains method has a key identical to the key present in the map stated above so, it returns true.
Example :
// Scala program of contains() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val map: Map[Int,Int] = Map(4 -> 7) // Applying contains method val result = map.contains(5) // Displays output println(result) } } |
false
Here, contains method has a key which is not identical to the key present in the map stated above so, it returns false.
Recommended Posts:
- Scala Map take() method with example
- Scala Map max() method with example
- Scala | map() method
- Scala Map min() method with example
- Scala Map get() method with example
- Scala Map last() method with example
- Scala Map iterator method with example
- Scala Iterator next() method with example
- Scala Map size() method with example
- Method Overloading in Scala
- Scala Map empty() method with example
- Scala Iterator seq() method with example
- Scala Map addString() method with example
- Scala | flatMap Method
- Scala Map tail() method with example
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.



