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 ObjectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating mapsvalgroup1=Map("Nidhi"->23,"Rahul"->18)valgroup2=Map("Geeta"->22,"Rahul"->18)// using ++ as a methodvalconcatenate=group1.++(group2)// Displays concatenated mapprintln("Concatenation is: "+ concatenate)}}chevron_rightfilter_noneOutput:
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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating mutable mapvalm=scala.collection.mutable.Map[String, Int]("Geeta"->21,"Nidhi"->23)// using <b>-</b> as a methodvalc=m.-("Geeta")// Displays a new mapprintln("The new Map returns: "+ c)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Rahul"->18)valn=Map("Geeta"->22,"Rahul"->18)// using 'get' as a methodvalx=m.get("Rahul")valy=n.get("Nidhi")// Displays key corresponding// to the given valuesprintln(x)println(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Rahul"->18)valn=Map("sonu"->16,"Nisha"->21)// using 'iterator' as a methodvalx=m.iteratorvaly=n.iterator// Displays if the iterator// is empty or notprintln(x)println(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Nisha"->21)valn=Map("sonu"->16,"Rahul"->18)// using 'addString' as a methodvalx=m.addString(newStringBuilder())valy=n.addString(newStringBuilder())// Displays elements in the// StringBuilderprintln(x)println(y)}}chevron_rightfilter_noneOutput:
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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Nisha"->21)valn=Map("sonu"->16,"Rahul"->18)// using 'addString' as a method// and adding a separator to itvalx=m.addString(newStringBuilder(),"_")valy=n.addString(newStringBuilder(),"_")// Displays elements in the// StringBuilder with the// separatorprintln(x)println(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Nisha"->21)valn=Map("sonu"->16,"Rahul"->18)// using 'apply' methodvalx=m.apply("Nisha")valy=n.apply("sonu")// Displays values of// the keyprintln(x)println(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating mutable mapvaln=scala.collection.mutable.Map("Nidhi"->23,"Nisha"->21)// using 'clear' methodvalx=n.clear()//Displays empty Mapprintln(x)}}chevron_rightfilter_noneOutput:()
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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating mutable mapvaln=scala.collection.mutable.Map("Nidhi"->23,"Nisha"->21)// using 'clone' methodvalx=n.clone()// Displays copied keysprintln(x)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Rahul"->18)valn=Map("sonu"->16,"Nisha"->21)// using 'contains' methodvalx=m.contains("Nidhi")valy=n.contains("Rahul")// Displays true if the key// is present else falseprintln(x)println(y)}}chevron_rightfilter_noneOutput:
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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18)// Creating Arrayvalx:Array[Any]=Array(0,0,0,0,0)// using 'copyToArray' methodm.copyToArray(x)// Displays keys copied in// the Arrayfor(m1<-x)println(m1)}}chevron_rightfilter_noneOutput:(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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18)// using 'count' methodvaly=m.count(z=>true)// Displays number of keys// in the Mapprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'drop' methodvaly=m.drop(2)// Displays all the elements of// the map except the first two// elementsprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'dropRight' methodvaly=m.dropRight(2)// Displays all the keys of// map except the last two// elementsprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'dropWhile' methodvaly=m.dropWhile(z=>true)// Displays empty mapprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'empty' methodvaly=m.empty// Displays empty mapprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapsvalm=Map("Nidhi"->23,"Rahul"->18)valn=Map("Nisha"->21,"Rohit"->16)// using 'equals' methodvaly=m.equals(n)// Displays true if the maps are// equal else returns falseprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'init' methodvaly=m.init// Displays all the elements// except the last oneprintln(y)}}chevron_rightfilter_noneOutput: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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating Mapvalm=Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'last' methodvaly=m.last// Displays the last elementprintln(y)}}chevron_rightfilter_noneOutput:(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 objectobjectGFG{// Main methoddefmain(args:Array[String]){// Creating mutable mapvalm=scala.collection.mutable.Map("Nidhi"->23,"Rahul"->18,"Nisha"->21,"Rohit"->16)// using 'remove' methodvaly=m.remove("Rahul")// Displays the value associated// with the key in the argumentprintln(y)}}chevron_rightfilter_noneOutput: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.

