0

I currently have a variable that looks like this:

val someVal = new HashMap[Float, Set[String]] with MultiMap[Float, String]

Now I would like to have a hash of these hashes of the form:

val someHashOfSomeVal = new HashMap[String, HashMap[Float, Set[String]] with MultiMap[Float, String]]

In other words, I need to have a hash table (with multiple values for each key) of hash tables (with multiple values of each key). Can anyone help me with how I declare / mutate this variable?

Do I mutate it like this?someHashOfSomeVal.addBinding("someKey", someVal)

1 Answer 1

2

It's unclear to me why you'd want the top-level map to have multiple values (other maps, in this case) per key, or how that would work in practice. I'll assume that you only want MultiMap at the lower level, in which case you can use the following approach:

import scala.collection.mutable.{ HashMap, MultiMap, Set }    

class TwoLevel[A, B, C] extends HashMap[A, MultiMap[B, C]] {
  override def default(key: A) = new HashMap[B, Set[C]] with MultiMap[B, C]
  def addTriple(a: A, b: B, c: C) {
    this += a -> this(a).addBinding(b, c)
  }

  def lookupPair(a: A, b: B): Set[C] = this(a).getOrElse(b, Set.empty)
}

val m = new TwoLevel[String, Float, String]

m.addTriple("a", 1.0F, "b")
m.addTriple("a", 0.0F, "c")

println(m.lookupPair("a", 0.0F))

Which prints Set(c), as you'd expect.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.