score:11
You're probably using immutable.Map
. You need to use mutable.Map
, or replace the set instead of modifying it with another immutable map.
Here's a reference of a description of the mutable vs immutable data structures.
So...
import scala.collection.mutable.Map
var m = Map[Int,Set[Int]]()
m += 1 -> Set(1)
m(1) += 2
score:4
In addition to @Stefan answer: instead of using mutable Map, you can use mutable Set
import scala.collection.mutable.{Set => mSet}
var m = Map[Int,mSet[Int]]()
m += 1 -> mSet(1)
m(1)+=2
mSet is a shortcut to mutable Set introduced to reduce verbosity.
scala> m
res9: scala.collection.immutable.Map[Int,scala.collection.mutable.Set[Int]] = Map(1 -> Set(2, 1))
score:4
I think what you really want here is a MultiMap
import collection.mutable.{Set, Map, HashMap, MultiMap}
val m = new HashMap[Int,Set[Int]] with MultiMap[Int, Int]
m.addBinding(1,1)
m.addBinding(1,2)
m.addBinding(2,3)
Note that m
itself is a val
, as it's the map itself which is now mutable, not the reference to the map
At this point, m
will now be a:
Map(
1 -> Set(1,2),
2 -> Set(3)
)
Unfortunately, there's no immutable equivalent to MultiMap, and you have to specify the concrete subclass of mutable.Map
that you'll use at construction time.
For all subsequent operations, it's enough to just pass the thing around typed as a MultiMap[Int,Int]
Source: stackoverflow.com
Related Query
- Adding element to a scala set which is a map value
- Adding element to scala set which is a IMMUTABLE map
- Adding value to Scala map
- How to get a scala Map value, when the key is a tuple in which only the first element is known?
- Scala map list element to a value calculated from previous elements
- Scala Collections: Using a value of Set find the key from a Map object.
- efficent way Add key and value to Map of Set in scala
- Create A singletonSet Function Which Creates A Singleton Set From One Integer Value In SCALA
- Scala adding the result of a function call to the value in a map
- Reverse a Map which has A Set as its value using HOF
- Adding all fields from a Map value to Json in Scala
- Map frequently changing JSON value to corresponding element in Scala
- filter scala map of key as string and value as set of objects
- how to remove key value from map in scala
- Optionally adding items to a Scala Map
- How to get a random element from a Set in Scala
- How to find a matching element in a list and map it in as an Scala API method?
- Scala Convert Set to Map
- How map only left value from scala Either?
- Getting the maximum key value pair in a Scala map by value
- Nice way to add number to element in Scala map if key exists or insert new element it not
- How to get min by value only in Scala Map
- scala main returns unit. How to set program's return value
- Set default value for function parameter in scala
- Scala - Convert map key value pair to string
- How to check which parameters of case class have default value using scala reflection 2.10
- Adding to scala map within for loop and conditional statement
- How to select a object field for map value in Scala using groupby
- Scala count number of occurences of an element in a Map
- Transform list in to map of element -> list(element) in scala
More Query from same tag
- Regex not matching
- Difference between Scala pattern matching using variable binding and guard
- play framework 2.6 ws cannot resolve
- Too many arguments for method += with Slick-generated code
- Scala - Convert String to Date in Spark RDD
- Scala REPL startup command line
- "dynamically" creating case classes with macros
- how to set JVM property line.separator to newline from bash
- How to deploy a Scala project from Eclipse?
- installing jdk/sbt on new ubuntu machine
- Dbpedia extraction framework - how to strip mediawiki formatting markup
- How to create generic constraint based on dependent type?
- Singleton object with value parameters?
- JADE/SCALATE template error - InvalidSyntaxException
- Cucumber-scala BDD test with maven
- Why does the andThen of Future not chain the result?
- Using Bitly library in Scala
- filter on data which are numeric
- return value "constrained" by function type in Scala
- In scala shapeless, is it possible to use literal type as a generic type parameter?
- Scala : How to create Unit test case in Intellij
- In scala, can we use loops inside a recursive function?
- Scala: Linux like pipe with error handling using Either
- Returning unnecessary empty stings in Match Case Scala
- How to change jetty port for Scalatra
- What is MonoidAggregator in Algebird
- Parsing error when trying to parse comma-delimited key-value pairs with values containing list separated by commas
- What should be the href of a collection response if I am getting one single item of that collection?
- JSON Writes for Java List
- Multiply 2 sparse matrices