Is there such a thing as bidirectional maps in Scala?

后端 未结 3 977
栀梦
栀梦 2021-02-12 10:11

I\'d like to link 2 columns of unique identifiers and be able to get a first column value by a second column value as well as a second column value by a first column value. Some

3条回答
  •  失恋的感觉
    2021-02-12 10:34

    Here's a quick Scala wrapper for Guava's BiMap.

    import com.google.common.{collect => guava}
    import scala.collection.JavaConversions._
    import scala.collection.mutable
    import scala.languageFeature.implicitConversions
    
    class MutableBiMap[A, B] private (
        private val g: guava.BiMap[A, B] = new guava.HashBiMap[A, B]()) {
    
      def inverse: MutableBiMap[B, A] = new MutableBiMap[B, A](g.inverse)
    }
    
    object MutableBiMap {
    
      def empty[A, B]: MutableBiMap[A, B] = new MutableBiMap()
    
      implicit def toMap[A, B] (x: MutableBiMap[A, B]): mutable.Map[A,B] = x.g
    }
    

提交回复
热议问题