Updated April 14, 2023
Introduction to Scala Map Function
Scala Map Function also is known as HASH TABLE is a collection of Key/Value pairs called as Hash Tables. The Key is used to access the values associated with it. Values in a Map can be the same but keys are always unique. The map is a higher-order function, which takes some function as a parameter and applies the function to every element of the source collection. The return type is the same as that of the source type of collection.
There are two kinds of MAP:
1. Immutable Map
2. Mutable Map:- By using scala.collection.mutable.Map
By Default Scala uses the Immutable Map, for using the mutable Map we need to import the Scala class explicitly.
Syntax
Below is the syntax for Scala Map Function:
Def map[B](f: ((K, V)) => B): Iterable[B]
Def map[K2, V2](f: ((K, V)) => (K2, V2)): Map[K2, V2]
For Immutable:
Var a = Map(key_1 -> value_1, key_2 -> value_2, ....)
For Mutable:
Var a = scala.collection.mutable.Map(key_1 -> value_1, key_2 -> value_2,....)
def map[B](f: (A) ⇒ B): Traversable[B]
How does Scala Map Function work?
Map Functions takes the function provided and applies to every element in the collection. So whatever the function element is provided MAP functions apply it to every element in the collection.
The source collection over which the MAP function is used remains unaffected.
We can do many operations over the collection with the help of MAP function in one shot.
Examples to Implement Scala Map Function
Now let us see that with the help of one example:
Example #1
Code:
Scala >val a = List (1,2,3,4,5)
a: List[Int] = List(1, 2, 3, 4, 5)
Scala > a.map(x=>x+2)
res0: List[Int] = List(3, 4, 5, 6, 7)
Output:
Explanation: Here we can see that the map function takes up all the values that are in the val a and operates it over the elements providing with a new List of elements without affecting the old one.
Example #2
Now let us see one more example of how Map Functions works by passing a function definition inside it.
Code:
scala> def add(a:Int):Int = { a+2}
add: (a: Int)Int
scala> a.map(add)
res1: List[Int] = List(3, 4, 5, 6, 7)
Output:
Explanation: Here we passed a user-defined function add and passed it to the MAP function, that generates a list with new values.
Usage of Scala Map Function
There are lots of usage of MAP function in Scala we will check some of its functionality and advantage with the help of examples.
1. Take() method in the Map
The take method in scala Map is used to return the first n elements of the map. Let us see take an example for that:
Code:
scala> val a = List(1,2,3,4,5,6)
a: List[Int] = List(1, 2, 3, 4, 5, 6)
scala> a.take(2)
res3: List[Int] = List(1, 2)
scala> a.take(3)
res4: List[Int] = List(1, 2, 3)
scala> a.take(0)
res5: List[Int] = List()
Output:
Explanation: Here we can see the take function returns all the elements in that list as per the value.
2. The last () method in MAP
This returns the last element of the MAP element:
Code:
scala> val a = List(1,2,3,4,5,6)
a: List[Int] = List(1, 2, 3, 4, 5, 6)
scala> a.last
res8: Int = 6
scala> a.map(x=>x+2)
res9: List[Int] = List(3, 4, 5, 6, 7, 8)
scala> res9.last
res10: Int = 8
Output:
Explanation: Here it returns the last element in the List.
3. Max and Min Function
It returns the Maximum and Minimum values in the Map:
Code:
scala> res9.max
res11: Int = 8
scala> res9.min
res12: Int = 3
Output:
Explanation: From the above example, it returns the Maximum and Minimum Map values.
4. GET () method
The get method is used to give the values associated with the keys .if no key is their NONE is returned.
Code:
scala> Val a = Map(1 -> "Sample" , 2 -> "Values" , 3 -> "Examples")
a: scala.collection.immutable.Map[Int,String] = Map(1 -> Sample, 2 -> Values, 3 -> Examples)
scala> a.get(1)
res13: Option[String] = Some(Sample)
scala> a.get(2)
res14: Option[String] = Some(Values)
scala> a.get(5)
res15: Option[String] = None
Output:
5. Is empty() MAP method
It checks whether the MAP is empty or not. It returns Boolean values with True and False based on Map values.
Code:
scala> a.isEmpty
res17: Boolean = false
6. Count Method
It returns all the keys satisfying that condition in the Map.
Code:
scala> a.count(z=>true)
res20: Int = 3
Output:
7. Init Method
This method returns all the values except the last one in the Map.
Code:
scala> val a = Map(1 -> "Sample" , 2 -> "Values" , 3 -> "Examples")
a: scala.collection.immutable.Map[Int,String] = Map(1 -> Sample, 2 -> Values, 3 -> Examples)
scala> a.init
res29: scala.collection.immutable.Map[Int,String] = Map(1 -> Sample, 2 -> Values)
Output:
8. Clone() method
It makes a copy of particular MAP values. It is a member of scala.collection.mutable.Map[String, Int]. So if we will use it with an immutable Map we will get an error.
Code:
scala> val a = scala.collection.mutable.Map(1 -> "Sample" , 2 -> "Values" , 3 -> "Examples")
a: scala.collection.mutable.Map[Int,String] = Map(2 -> Values, 1 -> Sample, 3 -> Examples)
scala> a.clone()
res2: scala.collection.mutable.Map[Int,String] = Map(2 -> Values, 1 -> Sample, 3 -> Examples)
Output:
Here we got a copy of the values with the help of clone function.
Code:
scala> val a = Map(1 -> "Sample" , 2 -> "Values" , 3 -> "Examples")
a: scala.collection.immutable.Map[Int,String] = Map(1 -> Sample, 2 -> Values, 3 -> Examples)
scala> a.clone()
<console>:13: error: method clone in class Object cannot be accessed in scala.collection.immutable.Map[Int,String]
Access to protected method clone not permitted because
prefix type scala.collection.immutable.Map[Int,String] does not conform to
object $iw where the access take place
a.clone()
Output:
Explanation: Here we see that there are many methods associated with the MAP function in scala and its usage with an example. So in this way MAP function is used in SCALA for functional programming approach.
Conclusion
Here from the above article, we saw how MAP function is used in SCALA and what are the methods that we can use in with this map function to make the programming efficient. MAP function does the operation with all the values present and get the result in a newer one without changing the actual value. So MAP is an optimized and useful method used for functional programming in Scala for various data loads.
Recommended Articles
We hope that this EDUCBA information on “Scala Map Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.