Scala 在Map上调用的方法

Scala 在Map上调用的方法

在Scala中,有很多方法可以在地图上调用。Scala方法是一个类的一部分,它有一个名称,一个签名,可以选择一些注释,以及任何字节代码。一个被解释为某个对象成员的函数被称为Method,而Map Method正好与Collection结合在一起,而且它是Traversable特性的成员,由Scala的Collection类执行(Traversable说明了许多具体的Method)。

Scala Map上调用的最主要的方法有以下几种

def ++(xs: Map[(A, B)]): Map[A, B]

这个方法被用来串联两个或更多的Map。在串联Map中,它将分离相同的键。

示例:

// Scala program to concatenate two Map
  
// Creating Object
object GFG 
{
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating maps
        val group1 = Map("Nidhi" -> 23, "Rahul" -> 18)
        val group2 = Map("Geeta" -> 22, "Rahul" -> 18)
  
        // using ++ as a method
        val concatenate = group1.++(group2)
  
        // Displays concatenated map
        println( "Concatenation is: " + concatenate)
    }
}

输出:

Concatenation is: Map(Nidhi -> 23, Rahul -> 18, Geeta -> 22)

这里,键 “Rahul “在两个Map中都存在,所以在串联两个Map时,类似的那张被删除。

def -(elem1: A, elem2: A, elems: A*): Map[A, B]

这个方法被用来删除参数中存在的键集。所以,它返回一个新的Map,包含这个Map的所有元素,除了这些参数。

示例:

// Scala program to delete keys
  
// Creating object
object GFG
{
      
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating mutable map 
        val m = scala.collection.mutable.Map[String, Int]("Geeta" -> 21, "Nidhi" -> 23) 
      
        // using <b>-</b> as a method
        val c = m.-("Geeta")
  
        // Displays a new map
        println( "The new Map returns: " + c)
    }
}

输出:

The new Map returns: Map(Nidhi -> 23)

因此,新Map只包含 “Nidhi”,”Geeta “被删除。

def get(key: A): Option[B]

该方法用于返回与方法中给出的参数值相对应的键。

示例:

// Scala program to get values
// corresponding to the key
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
        val n = Map("Geeta" -> 22, "Rahul" -> 18)
  
        // using 'get' as a method
        val x = m.get("Rahul")
        val y = n.get("Nidhi")
  
        // Displays key corresponding 
        // to the given values 
        println(x)
        println(y)
    }
}

输出:

Some(18)
None

在这里,”Rahul “返回其对应的值,但 “Nidhi “返回无,因为这个键与给定的地图没有关系。

def iterator: Iterator[(A, B)]

该方法用于返回一个迭代器。

示例:

// Scala program to return
// an iterator
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
        val n = Map("sonu" -> 16, "Nisha" -> 21)
  
        // using 'iterator' as a method
        val x = m.iterator
        val y = n.iterator
  
        // Displays if the iterator
        // is empty or not
        println(x)
        println(y)
    }
}

输出:

non-empty iterator
non-empty iterator

在这里,两个Maps都是非空的,所以会返回非空的迭代器。

def addString(b: StringBuilder): StringBuilder

这个方法被用来将Map中的每个元素添加到StringBuilder中。

示例:

// Scala program to add the elements
// of Map to the StringBuilder
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Nisha" -> 21)
        val n = Map("sonu" -> 16, "Rahul" -> 18)
  
        // using 'addString' as a method
        val x = m.addString(new StringBuilder())
        val y = n.addString(new StringBuilder())
  
  
        // Displays elements in the
        // StringBuilder
        println(x)
        println(y)
    }
}

输出:

Nidhi -> 23Nisha -> 21
sonu -> 16Rahul -> 18

因此,这些元素被返回到StringBuilder中。

def addString(b: StringBuilder, sep: String): StringBuilder

该方法将Map的元素添加到StringBuilder中,同时在元素之间添加分隔符。

示例:

// Scala program to add the elements
// of Map to the StringBuilder and 
// also add a separator
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Nisha" -> 21)
        val n = Map("sonu" -> 16, "Rahul" -> 18)
  
        // using 'addString' as a method
        // and adding a separator to it
        val x = m.addString(new StringBuilder(), "_")
        val y = n.addString(new StringBuilder(), "_")
  
        // Displays elements in the
        // StringBuilder with the 
        // separator
        println(x)
        println(y)
    }
}

输出:

Nidhi -> 23_Nisha -> 21
sonu -> 16_Rahul -> 18

因此,这些元素在StringBuilder中以分隔符的形式返回。

def apply(key: A): B
它有助于在Map中搜索一个键。

示例:

// Scala program to search 
// a key value
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Nisha" -> 21)
        val n = Map("sonu" -> 16, "Rahul" -> 18)
  
        // using 'apply' method
        val x = m.apply("Nisha")
        val y = n.apply("sonu")
  
        // Displays values of 
        // the key 
        println(x)
        println(y)
    }
}

输出:

21
16

在这里,如果搜索到的键不存在,那么键值就找不到。

def clear(): Unit

利用这个来清除Map。
注意:值clear是scala.collection.mutable.Map[String, Int]的一个成员。

示例:

// Scala program to clear
// the Map
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating mutable map
        val n = scala.collection.mutable.Map("Nidhi" -> 23, 
                                            "Nisha" -> 21)
  
        // using 'clear' method
        val x = n.clear()
  
        //Displays empty Map 
        println(x)
    }
}

输出:

()

在这里,一个可变的Map的键被删除。

def clone(): Map[A, B]

利用这个方法,可以对接收者对象进行复制。
注意:值克隆是scala.collection.mutable.Map[String, Int]的成员。

示例:

// Scala program to make
// a copy of the receivers 
// object
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating mutable map
        val n = scala.collection.mutable.Map("Nidhi" -> 23,
                                        "Nisha" -> 21)
  
        // using 'clone' method
        val x = n.clone()
  
        // Displays copied keys
        println(x)
    }
}

输出:

Map(Nidhi -> 23, Nisha -> 21)

这里,返回接收者对象的副本。

def contains(key: A): Boolean

这个方法用来检查键是否存在于Map中。如果键是存在的,则返回真,否则返回假。

示例:

// Scala program to check if 
// the key is present or not
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
        val n = Map("sonu" -> 16, "Nisha" -> 21)
  
        // using 'contains' method
        val x = m.contains("Nidhi")
        val y = n.contains("Rahul")
  
        // Displays true if the key 
        // is present else false
        println(x)
        println(y)
    }
}

输出:

true
false

这里,”Nidhi “存在于Map中,所以返回true,但 “Rahul “不存在于给定的Map中,所以返回false。

def copyToArray(xs: Array[(A, B)]): Unit

这个方法有助于将Map的一对键复制到数组中。

示例:

// Scala program to copy keys 
// to an Array
  
// Creating object
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
  
        // Creating Array
        val x: Array[Any] = Array(0, 0, 0, 0, 0)
  
        // using 'copyToArray' method
        m.copyToArray(x)
  
        // Displays keys copied in
        // the Array
        for(m1 <-x)
            println(m1)
    }
}

输出:

(Nidhi,23)
(Rahul,18)
0
0
0

这里,Map的两个键被复制到数组中。

def count(p: ((A, B)) = > Boolean): Int

该方法用于计算Map中的一对键。

示例:

// Scala program to count
// pair of keys in the Map
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
  
        // using 'count' method
        val y = m.count(z=>true)
  
        // Displays number of keys
        // in the Map
        println(y)
    }
}

输出:

2

这里,Map中存在两个键,所以返回两个。

def drop(n: Int): Map[A, B]

这个方法被用来删除前’n’个元素。

示例:

// Scala program to delete
// first n elements
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18,
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'drop' method
        val y = m.drop(2)
  
        // Displays all the elements of 
        // the map except the first two
        // elements
        println(y)
    }
}

输出:

Map(Nisha -> 21, Rohit -> 16)

这里,drop(n)是所需的操作,即删除第一个’n’元素,其余的元素被返回。

def dropRight(n: Int): Map[A, B]

这个方法被用来删除最后的’n’个元素。

示例:

// Scala program to delete 
// last n elements
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18, 
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'dropRight' method
        val y = m.dropRight(2)
  
        // Displays all the keys of 
        // map except the last two
        // elements
        println(y)
    }
}

输出:

Map("Nidhi" -> 23, "Rahul" -> 18)

这里,dropRight(n)是所需的操作,最后的’n’元素被删除,其余的元素被返回。

def dropWhile(p: ((A, B)) = > Boolean): Map[A, B]

这个操作会删除元素,直到所述条件得到满足。

示例:

// Scala program to delete the
// elements until the stated
// condition is satisfied
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18,
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'dropWhile' method
        val y = m.dropWhile(z=>true)
  
        // Displays empty map
        println(y)
    }
}

输出:

Map()

这里,dropWhile是所需的操作,根据给定的条件,将返回一个空的Map。

def empty: Map[A, B]
该方法用于返回一个空的Map。

示例:

// Scala program to form
// an empty Map
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18,
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'empty' method
        val y = m.empty
  
        // Displays empty map
        println(y)
    }
}

输出:

Map()

def equals(that: Any): Boolean

这个方法被用来检查两个Map是否有相同的键值对。

示例:

// Scala program to check if the
// two maps have the same 
// number of elements
  
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Maps
        val m = Map("Nidhi" -> 23, "Rahul" -> 18)
        val n = Map("Nisha" -> 21, "Rohit" -> 16)
  
        // using 'equals' method
        val y = m.equals(n)
  
        // Displays true if the maps are
        // equal else returns false
        println(y)
    }
}

输出:

false

在这里,如果两个Map的键值对相同,equals方法返回true,否则返回false。

**def init: Map[A, B]
这个方法用来返回地图的所有元素,除了最后一个。

示例:

// Scala program to return
// all the elements of the
// map except the last one
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18,
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'init' method
        val y = m.init
  
        // Displays all the elements
        // except the last one
        println(y)
    }
}

输出:

Map(Nidhi -> 23, Rahul -> 18, Nisha -> 21)

这里,在Map的四个元素中,Map的前三个元素被返回。

def last: (A, B)

该方法返回Map的最后一个元素。

示例:

// Scala program to find the 
// last element
  
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating Map
        val m = Map("Nidhi" -> 23, "Rahul" -> 18,
                    "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'last' method
        val y = m.last
  
        // Displays the last element
        println(y)
    }
}

输出:

(Rohit, 16)

def remove(key: A): Option[B]
该方法删除键并仅返回其值。

示例:

// Scala program to return the
// value of the given key
  
  
// Creating object 
object GFG
{
  
    // Main method
    def main(args: Array[String])
    {
  
        // Creating mutable map
        val m = scala.collection.mutable.Map("Nidhi" -> 23,
                "Rahul" -> 18, "Nisha" -> 21, "Rohit" -> 16)
  
        // using 'remove' method
        val y = m.remove("Rahul")
  
        // Displays the value associated
        // with the key in the argument
        println(y)
    }
}

输出:

some(18)

注意:删除值是可变的Map的成员。
这些是Scala的主要方法,还有很多这样的方法。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程