Dealing Options from a Map.get in Scala - scala

I've a Scala Map that contains a bunch of parameters that I get in a HTTP request.
val queryParams = Map( ("keyword" -> "k"), ("from" -> "f"), ("to" -> "t"), ("limit" -> "l") )
I've a method that takes all these parameters.
def someMethod( keyword: String, from: String, to: String, limit: String) = { //do something with input params }
I want to pass the parameters from the map into my method someMethod.
queryParams.get returns an Option. So I can call something like queryParams.get("keyword").getOrElse("") for each input parameter.
someMethod( queryParams.get("keyword").getOrElse(""), queryParams.get("from").getOrElse(""), queryParams.get("to").getOrElse(""), queryParams.get("limit").getOrElse(""))
Is there a better way ?

If all the parameters have the same default value you can set a default value to the entire Map:
val queryParams = Map( ("keyword" -> "k"), ("from" -> "f"), ("to" -> "t"), ("limit" -> "l") ).withDefaultValue("")
someMethod( queryParams("keyword"), queryParams("from"), queryParams("to"), queryParams("limit"))
withDefaultValue return a Map which for any non exist value return the default value. Now that you are sure you always get a value you can use queryParams("keyword") (without the get function).

Related

How to filter List of Maps in Scala

My list of Maps is like below
val myMap= List(
Map("name" -> "1st" , "status" -> "0"),
Map("name" -> "2nd" , "status" -> "1"),
Map("name" -> "3rd" , "status" -> "1")
)
I am trying to filter the list based on "status" = "1" and get another List of Maps with only name
So the output should be
Map("name" -> "2nd"),
Map("name" -> "3rd")
I am a beginner in scala, understand that I need to apply map,filter. But not getting how to proceed here.
If you are doing both filter and map the best solution is often to use collect:
myMap.collect{ case m if m.get("status").contains("1") => m - "status" }
Consider combining filter & map into collect:
val myMap = List(
Map("name" -> "1st", "status" -> "0"),
Map("name" -> "2nd", "status" -> "1"),
Map("name" -> "3rd", "status" -> "1"),
Map("xx" -> "4th", "status" -> "1"),
Map("name" -> "5th", "yy" -> "1")
)
myMap.collect{ case m if m.get("status").contains("1") && m.get("name").nonEmpty =>
Map("name" -> m("name"))
}
// List(Map(name -> 2nd), Map(name -> 3rd))
The guard clause in collect ensures that only those Maps consisting of key-value status->1 as well as key name will be included.
With filter and map you can write is as follows
myMap
.filter(m => m.get("status") == Some("1")) // filter maps with status 1
.map(m => "name" -> m.get("name").get) // use the name attribute to create a new map
Note that the get method returns an Option since the requested key might not exist. Therefore you need to compare with Some("1") and also use the .get on the result of m.get("name"). Using the .get method without checking that the option is not empty is not a good style, since this will fail at runtime if the map doesn't contain a name key.
A better approach which would omit these maps from the result could be
myMap
.filter(m => m.get("status") == Some("1"))
.flatMap(m => m.get("name").map(n => "name" -> n))
The map used here is from the Option class and will map a found name to Some("name" -> n) and None to None. The flatMap will finally unpack the Some and ignore the None elments

Scala Map - Use map function to replace key->value

I want to change the keys and values for the keys key1 and key2 only when their values are val1 and val2 (both these mappings should be present for the transformation to take place). I am able to do it using the following code, but I do not think this is very elegant or efficient.
Is there a better way to do the same thing, perhaps using just one .map function applied over map?
Code:
val map = Map(
"key1" -> "val1",
"key2" -> "val2",
"otherkey1" -> "otherval1"
)
val requiredKeys = List("key1", "key2")
val interestingMap = map.filterKeys(requiredKeys.contains) // will give ("key1" -> "val1", "key2" -> "val2").
val changedIfMatched =
if (interestingMap.get("key1").get.equalsIgnoreCase("val1") && interestingMap.get("key2").get.equalsIgnoreCase("val2"))
Map("key1" -> "newval1", "key2" -> "newval2")
else
interestingMap
print(map ++ changedIfMatched) // to replace the old key->values with the new ones, if any.
Also can ++ operation to update the old key->value mappings be made more efficient?
Just do the check ahead of time:
map
.get("k1").filter(_.equalsIgnoreCase("v1"))
.zip(map.get("k2").filter(_.equalsIgnoreCase("v2")))
.headOption
.fold(map) { _ =>
map ++ Map("key1" -> "newVal1", "key2" -> "newVal2")
}
Here's an approach that checks that both key value pairs match.
EDIT: Added a mapValues method to the Map class. This technique can be used to do further checks on the values of the map.
val m = Map("key1" -> "val1", "key2" -> "VAL2", "otherkey1" -> "otherval1")
val oldKVs = Map("key1" -> "val1", "key2" -> "val2")
val newKVs = Map("newkey1" -> "newval1", "newkey2" -> "newval2")
implicit class MapImp[T,S](m: Map[T,S]) {
def mapValues[R](f: S => R) = m.map { case (k,v) => (k, f(v)) }
def subsetOf(m2: Map[T,S]) = m.toSet subsetOf m2.toSet
}
def containsKVs[T](m: Map[T,String], sub: Map[T,String]) =
sub.mapValues(_.toLowerCase) subsetOf m.mapValues(_.toLowerCase)
val m2 = if (containsKVs(m, oldKVs)) m -- oldKVs.keys ++ newKVs else m
println(m2)
// Map(otherkey1 -> otherval1, newkey1 -> newval1, newkey2 -> newval2)
It takes advantage of the fact that you can convert Maps into Sets of Tuple2.
I think this will be the most generic and resuable solution for the problem.
object Solution1 extends App {
val map = Map(
"key1" -> "val1",
"key2" -> "val2",
"otherkey1" -> "otherval1"
)
implicit class MapUpdate[T](map: Map[T, T]) {
def updateMapForGivenKeyValues: (Iterable[(T, T)], Iterable[(T, T)]) => Map[T, T] =
(fromKV: Iterable[(T, T)], toKV: Iterable[(T, T)]) => {
val isKeyValueExist: Boolean = fromKV.toIterator.forall {
(oldKV: (T, T)) =>
map.toIterator.contains(oldKV)
}
if (isKeyValueExist) map -- fromKV.map(_._1) ++ toKV else map
}
}
val updatedMap = map.updateMapForGivenKeyValues(List("key1" -> "val1", "key2" -> "val2"),
List("newKey1" -> "newVal1", "newVal2" -> "newKey2"))
println(updatedMap)
}
So the method updateMapForGivenKeyValues takes the List of old key value and new key value tuple. If all the key value pairs mentioned in the first parameter of the method exist in the map then only we will update the map with new key value pairs mentioned in the second parameter of the method. As the method is generic will can be used on any data type like String, Int, some case class etc.
we can easily re-use the method for different type of maps without even changing a single line of code.
Answer to modified question
val map = Map(
"key1" -> "val1",
"key2" -> "val2",
"otherkey1" -> "otherval1"
)
val requiredVals = List("key1"->"val1", "key2"->"val2")
val newVals = List("newval1", "newval2")
val result =
if (requiredVals.forall{ case (k, v) => map.get(k).exists(_.equalsIgnoreCase(v)) }) {
map ++ requiredVals.map(_._1).zip(newVals)
} else {
map
}
This solution use forall to check that all the key/value pairs in requiredKeys are found in the map by testing each pair in turn.
For each key/value pair (k, v) it does a get on the map using the key to retrieve the current value as Option[String]. This will be None if the key is not found or Some(s) if the key is found.
The code then calls exists on the Option[String]. This method will return false if value is None (the key is not found), otherwise it will return the result of the test that is passed to it. The test is _.equalsIgnoreCase(v) which does a case-insensitive comparison of the contents of the Option (_) and the value from the requireKeys list (v).
If this test fails then the original value of map is returned.
If this test succeeds then a modified version of the map is return. The expression requiredVals.map(_._1) returns the keys from the requireVals list, and the zip(newVals) associates the new values with the original keys. The resulting list of values is added to the map using ++ which will replace the existing values with the new ones.
Original answer
val map = Map(
"key1" -> "val1",
"key2" -> "val2",
"otherkey1" -> "otherval1"
)
val requiredVals = Map("key1"->"val1", "key2"->"val2")
val newVals = Map("newkey1" -> "newval1", "newkey2" -> "newval2")
val result =
if (requiredVals.forall{ case (k, v) => map.get(k).exists(_.equalsIgnoreCase(v)) }) {
map -- requiredVals.keys ++ newVals
} else {
map
}
Note that this replaces the old keys with the new keys, which appears to be what is described. If you want to keep the original keys and values, just delete "-- requiredVals.keys" and it will add the new keys without removing the old ones.
You can use the following code:
val interestingMap =
if(map.getOrElse("key1", "") == "val1" && map.getOrElse("key2", "") == "val2")
map - "key1" - "key2" + ("key1New" -> "val1New") + ("key2New" -> "val2New")
else map
The check part(if statement) can be tweaked to suit your specific need.
if any of these key-value pairs are not present in the map, the original map will be returned, otherwise, you will get a new map with two updates at the requested keys.
Regarding efficiency, as long as there are only two keys to be updated, I do not think there is a real performance difference between using + to add elements directly and using ++ operator to overwrite the keys wholesale. If your map is huge though, maybe using a mutable map proves to be a better option in the long run.

How to find a map in list of maps where a key has a certain value in Scala?

I have a List[Map[String, Any] and I want to return the map which has a certain value for one of the keys. Here's an obfusticated example:
val items: List[Map[String, Any]] = List(Map("name" -> "A", "size" -> 50), Map("name" -> "B", "size" -> 75), Map("name" -> "C", "size" -> 100)))
val mapB: Map[String, Any] = items.find(|m| m("name") == "B") // Psuedo code
mapB would be Map("name" -> "B", "size" -> 75).
Bonus Question
Is there also a way to return the value of another key instead of the whole Map?
For example, if I only wanted the size value (75) for name B? I know I can extract it from the Map as a two-step operation, but if there's another more idiomatic way, I'd be interested in learning it.
Thanks!
find() returns an Option in case what you're looking for cannot be found.
val mapB: Option[Map[String, Any]] = items.find(_("name") == "B") //safe
or
val mapB: Map[String, Any] = items.find(_("name") == "B").get //unsafe, might throw
To retrieve the size value from the first Map where "name" -> "B" you could do something like this.
val sz :Any = items.find(m => m.get("name").contains("B")
&& m.get("size").nonEmpty).fold(0:Any)(_("size"))
Note that sz is type Any because that's what the type of the Map values. Which means that you'd have to cast it (discouraged) to type Int before you can do anything useful with it.
Avoid type Any. It's a sure sign that you're going in the wrong direction.
Complementing jwvh's answers (and using Tim's suggestion to improve safety).
Here is a function that addresses your bonus question.
def findAndGet(maps: List[Map[String, Any]], condition: (String, Any), key: String): Option[Any] = condition match {
case (k, v) =>
maps.find(map => map.get(k).contains(v)).flatMap(map => map.get(key))
}
findAndGet(maps = items, condition = ("name", "B"), key = "size")
// res0: Option[Any] = Some(75)
BTW, a Map[String, Any] makes me thing you are dealing with JSONs, have you take a look to any of the Scala libraries for dealing with them in a more "typesafe" way?, having an Any in your code should always be an alarm that something is wrong.

Functional Programming: Get new Map from existing Map with changed value

I have one map like
val strMap = Map[String, String]("a" -> "a1", "b" -> "b1") // Map(a -> a1, b -> b1)
and I want to create another map with same key but different value, based on value in strMap. For example
case class Data(data: String) {}
var dataMap = scala.collection.mutable.Map[String, Data]()
strMap.foreach (keyVal => {dataMap(keyVal._1) = Data(keyVal._2)})
val dataMapToUse = dataMap.toMap // Map(a -> Data(a1), b -> Data(b1))
but writing this in imperative style is causing issue like creation of "var dataMap", though I want to get immutable map. Because of this, I have to call toMap to get same.
How can I achieve same in functional programming?
Scala Version: 2.11
Why not simply use,
val dataMapToUse = strMap.map{case(k,v) =>(k -> Data(v))}

correct use of scala Map keyset

I am fussing about how to derive a key from a Scala Map.
The Map in question is simple:
The following function to derive a key from a Scala Map is here under:
def extractKey(myMap: Map[String, String]): String {
//process myMap
myMap = Map("SSN" -> "1")
//return "SSN"
}
val myMap = Map("Visa Number" "10")
How do I extract the string Visa Number from it?
Okay, I tried this much so far:
myMap.keySet and I derived a Set out of it as: scala.collection.immutable.Set[String] = Set("Visa Number")
I am a little confused about to proceed.
I tried the following:
myMap.keys
this returned a Iterable[String] = Set("Visa Number")
I also tried the following:
myMap.keysIterator that returned an Iterator[String]
So, what is the best way to derive a key out of the Map myMap
Now, what if I had a longer Map as:
myMapLonger = Map("SSN" -> "10", "AMEX" -> 11)
then how would i capture the keys and put them into a List?
thanks
Map("SSN" -> "10", "AMEX" -> 11).keys.toSeq
or
Map("SSN" -> "10", "AMEX" -> 11).toSeq.map(_._1)
One approach invloves applying unzip on the given Map,
val (keys,values) = Map("SSN" -> "10", "AMEX" -> 11).unzip
which delivers a duple of lists, the first entry with the keys, the second with the values
keys: List(SSN, AMEX)
values: List(10, 11))