Open In App

Scala | Methods to Call on a Map | Set-1

Last Updated : 29 Mar, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite- Scala Map.

In Scala there are foremost Methods to call on a Map. A Scala Method is a section of a class which has a designation, a signature, optionally a few annotations, and whatsoever byte-code. A function which is interpreted as a member of some Object is termed as a Method and the Map Method is exactly incorporated with Collections furthermore it is a member of a Traversable trait which is executed by the Collection Classes of Scala (Traversable explicates numerous concrete Methods).

The most predominant Methods to call on a Scala Map are as follows:

  • def ++(xs: Map[(A, B)]): Map[A, B]
    This Method is utilized to Concatenate two or more Maps. In Concatenating Maps it will separate the identical keys.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, the key “Rahul” is present in both the Map, so while Concatenating two Map, the similar one is removed.

  • def -(elem1: A, elem2: A, elems: A*): Map[A, B]
    This method is utilized to delete the set of keys present in the arguments. So, it returns a new map containing all the elements of this Map except these arguments.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Thus, the new map contains only “Nidhi” and “Geeta” is deleted.

  • def get(key: A): Option[B]
    This method is utilized to return the keys corresponding to the values given in the method as argument.
    Example:




    // 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)
        }
    }

    
    

    Output:

    Some(18)
    None
    

    Here, “Rahul” returns the its corresponding value but “Nidhi” returns None, as this key is not related to the Map given.

  • def iterator: Iterator[(A, B)]
    This method is utilized to return an iterator.
    Example:




    // 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)
        }
    }

    
    

    Output:

    non-empty iterator
    non-empty iterator
    

    Here, both the Maps are non-empty so, non-empty iterator is returned.

  • def addString(b: StringBuilder): StringBuilder
    This method is utilized to add each of the elements of the Map to the StringBuilder.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Thus, the elements are returned in the StringBuilder.

  • def addString(b: StringBuilder, sep: String): StringBuilder
    This method adds elements of the Map to the StringBuilder and also adds a separator between the elements.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Thus, the elements are returned in the StringBuilder with a separator.

  • def apply(key: A): B
    It is helpful in searching a key in the Map.
    Example:




    // 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)
        }
    }

    
    

    Output:

    21
    16
    

    Here, if the searched key does not exists then the key value is not found.

  • def clear(): Unit
    This is utilized to clear the Map.
    Note:value clear is a member of scala.collection.mutable.Map[String, Int].
    Example:




    // 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)
        }
    }

    
    

    Output:

    ()
    

    Here, keys of a mutable Map are removed.

  • def clone(): Map[A, B]
    This method is utilized to make a copy of the receivers object.
    Note:value clone is a member of scala.collection.mutable.Map[String, Int].
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, copy of the receivers object is returned.

  • def contains(key: A): Boolean
    This method is utilized to check if the key is present in the Map or not. If the key is present it returns true else returns false.
    Example:




    // 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)
        }
    }

    
    

    Output:

    true
    false
    

    Here, “Nidhi” is present in the Map so, true is returned but “Rahul” is not present in the given map so, it returns false.

  • def copyToArray(xs: Array[(A, B)]): Unit
    This method is helpful in copying pair of keys of the Map to an Array.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, two keys of the Map are copied to the Array.

  • def count(p: ((A, B)) => Boolean): Int
    This method is utilized to count pair of keys in the Map.
    Example:




    // 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)
        }
    }

    
    

    Output:

    2
    

    Here, two keys are present in the Map so, two is returned.

  • def drop(n: Int): Map[A, B]
    This method is utilized to delete the first ‘n’ elements.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, drop(n) is the desired operation, where first ‘n’ elements are deleted and rest of the elements are returned.

  • def dropRight(n: Int): Map[A, B]
    This method is utilized to delete the last ‘n’ elements.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, dropRight(n) is the desired operation, where last ‘n’ elements are deleted and rest of the elements are returned.

  • def dropWhile(p: ((A, B)) => Boolean): Map[A, B]
    This operation deletes the elements until the stated condition is satisfied.
    Example:




    // 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)
        }
    }

    
    

    Output:

     Map()
    

    Here, dropWhile is the desired operation and according to the given condition, an empty Map is returned.

  • def empty: Map[A, B]
    This method is utilized to return an empty Map.
    Example:




    // 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)
        }
    }

    
    

    Output:

     Map()
    
  • def equals(that: Any): Boolean
    This method is utilized to check if the two maps have the same key-values pair.
    Example:




    // 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)
        }
    }

    
    

    Output:

     false
    

    Here, equals method returns true if the key-value pairs of both the Maps are same else returns false.

  • def init: Map[A, B]
    This method is utilized to return all the elements of the Map except the last one.
    Example:




    // 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)
        }
    }

    
    

    Output:

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

    Here, out of four elements of the Map, the first three elements of the Map are returned.

  • def last: (A, B)
    This method returns the last element of the Map.
    Example:




    // 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)
        }
    }

    
    

    Output:

    (Rohit, 16)
    
  • def remove(key: A): Option[B]
    This method drops the key and return its value only.
    Example:




    // 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)
        }
    }

    
    

    Output:

    some(18)
    

    Note:The value remove is member of the mutable Map.
    These were the major methods of Scala, there are many more such methods.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads