Scala的contains()方法與Scala的isDefinedAt方法等效,但是唯一的區別是,在所有PartialFunction類上都可以看到isDefinedAt,而contains明確定義為Scala的SortedMap接口。它檢查指定的SortedMap是否包含鍵的綁定。
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.
範例1:
// Scala program of contains()
// method
import scala.collection.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)
}
}
輸出:
true
在這裏,contains方法的鍵與上述SortedMap中存在的鍵相同,因此它返回true。範例2:
// Scala program of contains()
// method
import scala.collection.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)
}
}
輸出:
false
在這裏,contains方法具有與上述SortedMap中存在的鍵不同的鍵,因此它返回false。
相關用法
- Scala Mutable SortedMap toArray()用法及代碼示例
- Scala Mutable SortedMap toSeq()用法及代碼示例
- Scala Mutable SortedMap toString()用法及代碼示例
- Scala Mutable SortedMap toList()用法及代碼示例
- Scala Mutable SortedMap takeRight()用法及代碼示例
- Scala Mutable SortedMap take()用法及代碼示例
- Scala Mutable SortedMap tail()用法及代碼示例
- Scala Mutable SortedMap transform()用法及代碼示例
- Scala Mutable SortedMap mkString()用法及代碼示例
- Scala Mutable SortedMap mkString()用法及代碼示例
- Scala Mutable SortedMap min()用法及代碼示例
- Scala Mutable SortedMap init()用法及代碼示例
- Scala Mutable SortedMap drop()用法及代碼示例
- Scala Mutable SortedMap count()用法及代碼示例
- Scala Mutable SortedMap equals()用法及代碼示例
- Scala Mutable SortedMap empty()用法及代碼示例
- Scala Mutable SortedMap dropWhile()用法及代碼示例
- Scala Mutable SortedMap get()用法及代碼示例
注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Scala Mutable SortedMap contains() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。