Reputation: 585
I thought this was going to be relatively straightforward. So I will explain:
I have a Map as under: This what I tried in the REPL
scala> val entriesList = List(("tPolicyId" -> "MasterCard"), ("SSN" -> "0"), ("MasterCard" -> "3"));
entriesList: List[(String, String)] = List((tPolicyId,MasterCard), (US SSN,0), (MasterCard,3))
I then convert the list to a Map as below.
scala> val entriesMap = entriesList.toMap
entriesMap: scala.collection.immutable.Map[String,String] = Map(tPolicyId -> MasterCard,SSN -> 0, MasterCard -> 3)
So far so good. Now what I want to accomplish is a way to iterate over this Map, have a guard condition or a predicate as: if the function I use to iterate over this Map encounters a key called tPolicyMap, please return or yield a new immutable Map, newMap that looks like this:
***newMap: scala.collection.immutable.Map[String,String] = Map(tPolicyId -> MasterCard)***
I thought this was going to be easy and the below represents all my failed attempts so far.
scala> val tPolicyMap = for (entry <- entriesMap if entry.contains("tPolicyId")) yield entry
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap if entry.contains("tPolicyId")) yield entry
^
scala> val tPolicyMap = for (entry <- entriesMap if ( entry.contains("tPolicyId"))) yield entry
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap if ( entry.contains("tPolicyId"))) yield entry
^
scala> val tPolicyMap = for (entry <- entriesMap if ( entry.contains("tPolicyId"))) yield (entry)
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap if ( entry.contains("tPolicyId"))) yield (entry)
^
scala> val tPolicyMap = for (entry <- entriesMap; if ( entry.contains("tPolicyId"))) yield (entry)
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap; if ( entry.contains("tPolicyId"))) yield (entry)
^
scala> val tPolicyMap = for (entry <- entriesMap; if ( entry contains("tPolicyId"))) yield (entry)
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap; if ( entry contains("tPolicyId"))) yield (entry)
^
scala> val tPolicyMap = for (entry <- entriesMap; if entry contains("tPolicyId"))) yield entry
<console>:1: error: illegal start of simple expression
val tPolicyMap = for (entry <- entriesMap; if entry contains("tPolicyId"))) yield entry
^
scala> val tPolicyMap = for (entry <- entriesMap; if entry contains("tPolicyId")) yield entry
<console>:10: error: value contains is not a member of (String, String)
val tPolicyMap = for (entry <- entriesMap; if entry contains("tPolicyId")) yield entry
^
scala> val tPolicyMap = for (entry <- entriesMap) yield entry
tPolicyMap: scala.collection.immutable.Map[String,String] = Map(tPolicyId -> MasterCard/Diner's Club U
S Number, US SSN -> 0, MasterCard -> 3)
scala> val tPolicyEntry = entriesMap.contains("tPolicyId")
tPolicyEntry: Boolean = true
scala> val tPolicyEntry = if (entriesMap.contains("tPolicyId")) yield entriesMap
<console>:1: error: illegal start of simple expression
val tPolicyEntry = if (entriesMap.contains("tPolicyId")) yield entriesMap
^
scala> val tPolicyEntry = for(entry <- entriesMap; if entry == "tPolicyId" yield entry
<console>:1: error: ')' expected but 'yield' found.
val tPolicyEntry = for(entry <- entriesMap; if entry == "tPolicyId" yield entry
^
scala> val tPolicyEntry = for(entry <- entriesMap; if entry == "tPolicyId") yield entry
<console>:10: warning: comparing values of types (String, String) and String using `==' will always yield false
val tPolicyEntry = for(entry <- entriesMap; if entry == "tPolicyId") yield entry
^
tPolicyEntry: scala.collection.immutable.Map[String,String] = Map()
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry
<console>:1: error: illegal start of simple expression
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry
^
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)))) yield entry
<console>:1: error: illegal start of simple expression
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)))) yield entry
^
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry)
<console>:1: error: illegal start of simple expression
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry)
^
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry))
<console>:1: error: illegal start of simple expression
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry)) yield entry))
^
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry) yield entry))
<console>:1: error: ')' expected but 'yield' found.
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry) yield entry))
^
scala> val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry) yield entry)
<console>:1: error: ')' expected but 'yield' found.
val tPolicyEntry = entriesMap.keys.foreach( (entry) => if(entriesMap.contains(entry) yield entry)
As you can see, I have tried unsuccessfully to use filter, yield, foreach, for, etc in various combinations. I still don't get it/
So, I have tried hard, and I am probably making some fundamentally naive mistakes.
If someone can help me figure out how to get this Map I want, I would much appreciate the learning experience
thanks in advance
Upvotes: 0
Views: 517
Reputation: 2821
The filter
solution is good. You can also do collect
which might be handy if you plan to do a map
after the filter
entriesMap.collect{ case(k, v) if k == "tPolicyId" => (k, s"test${v}") } // example instead of plain (k, v)
// scala.collection.immutable.Map[String,String] = Map(tPolicyId -> testMasterCard)
Is the same as:
(entriesMap filterKeys { _ == "tPolicyId" }).map((kv) => (kv._1, s"test${kv._2}"))
// scala.collection.immutable.Map[String,String] = Map(tPolicyId -> testMasterCard)
Upvotes: 0
Reputation: 4999
Its as simple as
entriesMap.filter { case (k, _) => k == "tPolicyId" }
//> res0: scala.collection.immutable.Map[String,String] = Map(tPolicyId -> MasterCard)
Or simply
entriesMap filterKeys { _ == "tPolicyId" }
Upvotes: 1
Reputation: 11479
You are thinking imperatively (iterate, compare, if matching then do this) as opposed the higher level that the Scala immutable collections leans towards:
// get looks up value from a map using the key (fast)
// will give you an Option[String] back -
// Some(value) if the key exists, None if it doesn't
tpPolicyMap.get("tPolicyId").fold[Map[String, String]](
// result for None - no "tPolicyId"-key in the source map
Map.empty
)(value =>
// found, create a new map with that entry
Map("tPolicyId" -> value)
)
Upvotes: 2