Many-value map in Scala -


In Scala 2.8, I have the unchanging map with multiple values ​​for each key: < / P>

  map [t, iterable [u]]  

Is there a better representation? Second, how would you generate such a map

  Iterable [(T, U)]  

? I am currently using:

  def to groupedMap [T, U] (vals: Iterable [(T, U)]: map [T, Iterable [U]] = Vals.groupBy (_ ._1) .map ({case (s, this) => (s, imp (_._ 2)}}).  

that works,

edit: I should specify that I am working with irreversible data. Is there an irreversible equivalent for multimax?

If you do not really need any immutability, then say as the other, multimap The way to go is if you really need immutability, then the approach that you have taken is as easy as anything else; There is nothing in (AAAAIC), and any composition of an immutable multimet is going to work a lot in comparison to the method you get.

Representation is best or not used. Do you often want to work with all the values ​​in conformity with a key? Will you put the same price on your map? If yes, then your representation is correct.

If you want to include the highest value on one key at most, then you should use set [u] instead inappropriate [u] < / Code> (which can be easily done by adding .toSet to it.map (_._ 2) .).

If you disliked dealing with set / etres and just putting it together (i.e. you actually added significant value than the key setflu pair only), then you have a cover around it. The class will write a map that presents a map interface and +, - and works right with the iterator.

Here is an example that I had guessed a bit more time than it was (here is formatted to cut and paste (RPLL):

  import Scala.collection._ class MapSet [A, B] (Well set: map [A, set [B]] = map [A, set [B]] ()) MapLike [A, B, MapSet [A, B] ]] Extends the map [A, B] to {def get (key: A) = sets.getOrElse (key, Set [B] ()). HeadOption def iterator = New Iterator [(A, B)] {Private Val Seti = sets.iterator Private Var: Options [A] = No Private War Singles It is: Iterator [B] = NIEL. Motivational personal def ready next {while (seti.hasNext &! Singles.hasNext) {val kv = seti.next thiskey = some (kv._1) singles = kv._2. Iterator}} def hasNext = {if (singles hasNext) true else {readyNext singles.hasNext}} def next = {If (singles.hasNext) (thiskey.get, singles.next) other {readyNext (thiskey.get, singles. Next>}} def + [B1>: B] (KV: (A, B1)): MapSet [A, B] = {Val Value: B = KV._2.AS Instanceoff [B] new MAPSet (Set + + ((KV._1, set.gateolelse (kv._1, set [b] ()) + value (kV: (a, b)): mapet [a, b] = {val found = set .gget (Kv._1) if (found. ESepti || Got.get.contains (kv._2)) and this new mapset (set + ((kv._1, got.get - kv._2))}} Override Def Rick = New Mapset (Map [A, Set [B] ] ())}  

And we can see that this work is desired like this:

  Scala> New mapet () ++ List (1-> "hi", 2-> "there", 1-> "hello", 3-> "bye") res0: scala.collection.map [int, Java.lang.String] = map (1 -> Hi, 1 -> Hello, 2 - & gt; there, 3 -> By Scale & gt; Res0 + (2-> "ya") res1: scala.collection.Map [int , Java.lang.String] = Map (1 -> Hi, 1 -> Hello, 2 -> there, 2 ->, 3 - & gt; Bye) Scala> Res1 - 1 res2: scala.collection.ma P [int, java.lang.String] = map (2 -> there, 2 ->, 3 -> by)  

(Although if you + After + you want to get a masket back, you have to override +; in the map hierarchy your own builders do not have to take care of such things).


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -