Scala Map drop() 方法与示例
drop()方法用于删除前“n”个元素。
Method Definition: def drop(n: Int): Map[A, B]
Return Type: It returns all the elements of the map except the first ‘n’ elements.
示例 #1:
// Scala program of drop()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map("geeks" -> 5, "for" -> 3, "cs" -> 5)
// Applying drop method
val result = m1.drop(1)
// Displays output
println(result)
}
}
输出:
Map(for -> 3, cs -> 5)
示例 #2:
// Scala program of drop()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a map
val m1 = Map("geeks" -> 5, "for" -> 3, "geeks" -> 5)
// Applying drop method
val result = m1.drop(1)
// Displays output
println(result)
}
}
输出:
Map(for -> 3)
因此,两个相同的键都从地图中删除。