Created
May 13, 2012 06:58
-
-
Save outsideris/2686592 to your computer and use it in GitHub Desktop.
implement multimap in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pis.chap22.multimap | |
import scala.collection.immutable.HashMap | |
class LasMultiMap[A, B] { | |
var mmap = new HashMap[A, List[B]] | |
def put(key:A, value:B) = { | |
if (mmap.contains(key)) { | |
mmap = mmap + ( (key, mmap.get(key).toList(0) ::: List(value) )) | |
} else { | |
mmap = mmap + ( (key, List(value)) ) | |
} | |
} | |
def apply(key:A):List[B] = { | |
if (mmap.get(key).isEmpty) { | |
Nil | |
} else { | |
mmap.get(key).toList(0) | |
} | |
} | |
def update(key:A, value:B) = { | |
put(key, value) | |
} | |
def keySet() = { | |
mmap.keySet | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment