score:36
Try this:
import collection.mutable.HashMap
val x = new HashMap[Int,String]() { override def default(key:Int) = "-" }
x += (1 -> "b", 2 -> "a", 3 -> "c")
Then:
scala> x(1)
res7: String = b
scala> x(2)
res8: String = a
scala> x(3)
res9: String = c
scala> x(4)
res10: String = -
score:0
I'm more a java guy... but if getOrElse
is not final, why don't you just extend HasMap
and provide something like this:
override def getOrElse(k: Int, default: String) = {
return super.getOrElse(k,"_")
}
Note: syntax is probably screwed up but hopefully you'll get the point
score:1
scala> val x = HashMap(1 -> "b", 2 -> "a", 3 -> "c").withDefaultValue("-")
x: scala.collection.immutable.Map[Int,java.lang.String] = Map((1,b), (2,a), (3,c))
scala> x(3)
res0: java.lang.String = c
scala> x(5)
res1: java.lang.String = -
EDIT:
For mutable.HashMap
, you could do the following:
scala> import collection.mutable
import collection.mutable
scala> val x = new mutable.HashMap[Int, String] {
| override def apply(key: Int) = super.get(key) getOrElse "-"
| }
x: scala.collection.mutable.HashMap[Int,String] = Map()
scala> x += (1 -> "a", 2 -> "b", 3 -> "c")
res9: x.type = Map((2,b), (1,a), (3,c))
scala> x(2)
res10: String = b
scala> x(4)
res11: String = -
There might be a better way to do this. Wait for others to respond.
score:72
Wow, I happened to visit this thread exactly one year after I posted my last answer here. :-)
Scala 2.9.1. mutable.Map
comes with a withDefaultValue
method. REPL session:
scala> import collection.mutable
import collection.mutable
scala> mutable.Map[Int, String]().withDefaultValue("")
res18: scala.collection.mutable.Map[Int,String] = Map()
scala> res18(3)
res19: String = ""
Source: stackoverflow.com
Related Query
- Scala: Using HashMap with a default value
- Method call with option value or default parameter in Scala
- How to check which parameters of case class have default value using scala reflection 2.10
- Update a mutable map with default value in Scala
- Genaric type parsing in Scala with default value
- Scala Map with mutable default value always point to the same object
- Options in Scala with Boolean default value with getOrElse
- Scala class constructor default arguments with expression using previous members
- adding column with the length of other column as value using scala
- Scala : cannot check match for unreachibility even with default value
- Scala - List take - OrElse - with default value
- Scala function arguments with default value followed by multiarguments
- json Generic decoder with default values using scala with circe
- How to define a type of function with default value in Scala
- How to initialize a generic array with a default value in Scala
- Scala constructor with default value
- Scala quasiquote generating parameter default value with backticks
- "Forward reference extends over definition value exp" when using an abstract class with case classes of a Scala tutorial
- Scala dataframe: replace spaces with null value using regexp_replace
- How to invoke default constructor using scala reflection if we have default value defined in our constructor
- Is there any style guidelines about using implicit parameter with default value in scala?
- scala - create a mutable map and with default value as (0,0) if key not exist
- Split string with default value in scala
- Scala declare class with default value for field
- Scala - conditional product/join of two arrays with default values using for comprehensions
- Scala - Map with default value None?
- How to fill column with value taken from a (non-adjacent) previous row without natural partitioning key using Spark Scala DataFrame
- Compilation issue with enum in Scala using type with value
- Scala Spark. Create object with default value by DataType
- Find average value from a column of stream dataframe with array values using spark scala
More Query from same tag
- How to create an optional instance of a case class?
- scala.tools.nsc.IMain within Play 2.1
- Zipkin failing to start
- FS2 Running streams in sequence
- How to get the actor system reference from inside the actor
- Pass multiple conditions as a string in where clause in Spark
- What does the : mean after a value in Scala?
- Object identity of listeners
- scala overriding class parameters
- Scala: Map column integer values in a dataframe
- Scala as a shell script: jars on the classpath
- Pushing Spark Streaming RDDs to Neo4j -Scala
- Scala solution to nQueen using for-comprehension
- Generic Akka ActorSystem
- Does collection.mutable.HashMap have an efficient size method?
- perform join on multiple DataFrame in spark
- Creating a more specific implicit using a structural type with Scala
- Eclipse Project with Scala Plugin, Maven and Spark
- Code coverage 0% with sonarqube 8.9 upgrade - scala project
- Constructor can not be instantiated
- Weird scala errors. found: scala.Double required Double
- how to generate new column values for each group using a condition
- Create List[List[String]] from a text file separated by blank lines (using Regex)
- Access protobuf field name dynamically with scala
- Mutable Set += return value in Scala
- What is the forSome keyword in Scala for?
- What scala expects as a value when it's method accepts '?0' as argument type?
- Iceberg's FlinkSink doesn't update metadata file in streaming writes
- How to join 2 spark sql streams
- Create nested Java TreeMap in Scala