1. Introduction
Maps in Scala are collections of key-value pairs, where each key maps to a value. Scala provides both mutable and immutable versions of Maps, with immutable being the default type. This blog post will divw into various methods available in Scala Maps, demonstrating how to effectively use and manipulate these collections.
Scala - Map Methods
Scala Map offer a wide range of methods for manipulating key-value pairs:
- get: Retrieves the value associated with a given key.
- getOrElse: Retrieves the value for a key, or a default value if the key is not found.
- ++: Adds multiple key-value pairs to the map.
- --: Removes keys from the map.
- keys: Returns all keys in the map.
- values: Returns all values in the map.
- isEmpty: Checks if the map is empty.
- contains: Checks if a key is present in the map.
- updated: Creates a new map by updating an entry.
- mapValues: Transforms the values in the map.
- foreach: Iterates over each key-value pair in the map.
- size: Returns the number of key-value pairs in the map.
2. Program Steps
1. Create an example map.
2. Apply various map methods and store or print the results.
3. Execute the code to showcase each method's functionality.
3. Code Program
object MapMethodsDemo extends App { val myMap: Map[String, Int] = Map("apple" -> 1, "banana" -> 2, "cherry" -> 3) // Get a value println(s"Value for 'apple': ${myMap.get("apple")}") // Get a value with a default println(s"Value for 'orange' or default 0: ${myMap.getOrElse("orange", 0)}") // Add key-value pairs val newMap = myMap ++ Map("date" -> 4, "elderberry" -> 5) println(s"New map with added elements: $newMap") // Remove keys val reducedMap = myMap -- List("apple", "banana") println(s"Map after removing keys: $reducedMap") // Get all keys println(s"Keys: ${myMap.keys}") // Get all values println(s"Values: ${myMap.values}") // Check if map is empty println(s"Is the map empty? ${myMap.isEmpty}") // Check if map contains a key println(s"Does the map contain 'banana'? ${myMap.contains("banana")}") // Update a map val updatedMap = myMap.updated("cherry", 5) println(s"Map with updated value: $updatedMap") // Transform values val doubledValuesMap = myMap.mapValues(_ * 2) println(s"Map with doubled values: $doubledValuesMap") // Iterate over map print("Map iteration: ") myMap.foreach { case (key, value) => print(s"($key -> $value) ") } // Get map size println(s"\nMap size: ${myMap.size}") }
Output:
Value for 'apple': Some(1) Value for 'orange' or default 0: 0 New map with added elements: Map(apple -> 1, banana -> 2, cherry -> 3, date -> 4, elderberry -> 5) Map after removing keys: Map(cherry -> 3) Keys: Set(apple, banana, cherry) Values: Iterable(1, 2, 3) Is the map empty? false Does the map contain 'banana'? true Map with updated value: Map(apple -> 1, banana -> 2, cherry -> 5) Map with doubled values: Map(apple -> 2, banana -> 4, cherry -> 6) Map iteration: (apple -> 1) (banana -> 2) (cherry -> 3) Map size: 3
Explanation:
1. myMap.get("apple") retrieves the value associated with the key "apple".
2. myMap.getOrElse("orange", 0) provides a default value if the key is not found.
3. myMap ++ Map(...) demonstrates adding multiple key-value pairs.
4. myMap -- List(...) shows how to remove keys from the map.
5. myMap.keys and myMap.values list all keys and values in the map, respectively.
6. myMap.isEmpty checks if the map is empty.
7. myMap.contains("banana") verifies the presence of a key.
8. myMap.updated("cherry", 5) updates the value for a specific key.
9. myMap.mapValues(_ * 2) applies a function to all values in the map.
10. myMap.foreach iterates over each key-value pair.
11. myMap.size returns the number of elements in the map.
Comments
Post a Comment