Scala | Methods to Call on a Map | Set-1
Last Updated :
29 Mar, 2019
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:
object GFG
{
def main(args : Array[String])
{
val group 1 = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val group 2 = Map( "Geeta" - > 22 , "Rahul" - > 18 )
val concatenate = group 1 .++(group 2 )
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:
object GFG
{
def main(args : Array[String])
{
val m = scala.collection.mutable.Map[String, Int]( "Geeta" - > 21 , "Nidhi" - > 23 )
val c = m.-( "Geeta" )
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val n = Map( "Geeta" - > 22 , "Rahul" - > 18 )
val x = m.get( "Rahul" )
val y = n.get( "Nidhi" )
println(x)
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val n = Map( "sonu" - > 16 , "Nisha" - > 21 )
val x = m.iterator
val y = n.iterator
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Nisha" - > 21 )
val n = Map( "sonu" - > 16 , "Rahul" - > 18 )
val x = m.addString( new StringBuilder())
val y = n.addString( new 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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Nisha" - > 21 )
val n = Map( "sonu" - > 16 , "Rahul" - > 18 )
val x = m.addString( new StringBuilder(), "_" )
val y = n.addString( new StringBuilder(), "_" )
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Nisha" - > 21 )
val n = Map( "sonu" - > 16 , "Rahul" - > 18 )
val x = m.apply( "Nisha" )
val y = n.apply( "sonu" )
println(x)
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val n = scala.collection.mutable.Map( "Nidhi" - > 23 ,
"Nisha" - > 21 )
val x = n.clear()
println(x)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val n = scala.collection.mutable.Map( "Nidhi" - > 23 ,
"Nisha" - > 21 )
val x = n.clone()
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val n = Map( "sonu" - > 16 , "Nisha" - > 21 )
val x = m.contains( "Nidhi" )
val y = n.contains( "Rahul" )
println(x)
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val x : Array[Any] = Array( 0 , 0 , 0 , 0 , 0 )
m.copyToArray(x)
for (m 1 < -x)
println(m 1 )
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val y = m.count(z => true )
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.drop( 2 )
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.dropRight( 2 )
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.dropWhile(z => true )
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.empty
println(y)
}
}
|
- def equals(that: Any): Boolean
This method is utilized to check if the two maps have the same key-values pair.
Example:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 )
val n = Map( "Nisha" - > 21 , "Rohit" - > 16 )
val y = m.equals(n)
println(y)
}
}
|
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.init
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:
object GFG
{
def main(args : Array[String])
{
val m = Map( "Nidhi" - > 23 , "Rahul" - > 18 ,
"Nisha" - > 21 , "Rohit" - > 16 )
val y = m.last
println(y)
}
}
|
- def remove(key: A): Option[B]
This method drops the key and return its value only.
Example:
object GFG
{
def main(args : Array[String])
{
val m = scala.collection.mutable.Map( "Nidhi" - > 23 ,
"Rahul" - > 18 , "Nisha" - > 21 , "Rohit" - > 16 )
val y = m.remove( "Rahul" )
println(y)
}
}
|
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
Share your thoughts in the comments
Please Login to comment...