Is there such a thing as bidirectional maps in Scala?

后端 未结 2 1565
失恋的感觉
失恋的感觉 2021-02-12 09:50

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

相关标签:
2条回答
  • 2021-02-12 10:32

    Guava has a bimap that you can use along with

    import scala.collection.JavaConversions._
    
    0 讨论(0)
  • 2021-02-12 10:33

    My BiMap approach:

    object BiMap {
      private[BiMap] trait MethodDistinctor
      implicit object MethodDistinctor extends MethodDistinctor
    }
    
    case class BiMap[X, Y](map: Map[X, Y]) {
      def this(tuples: (X,Y)*) = this(tuples.toMap)
      private val reverseMap = map map (_.swap)
      require(map.size == reverseMap.size, "no 1 to 1 relation")
      def apply(x: X): Y = map(x)
      def apply(y: Y)(implicit d: BiMap.MethodDistinctor): X = reverseMap(y)
      val domain = map.keys
      val codomain = reverseMap.keys
    }
    
    val biMap = new BiMap(1 -> "A", 2 -> "B")
    println(biMap(1)) // A
    println(biMap("B")) // 2
    

    Of course one can add syntax for <-> instead of ->.

    0 讨论(0)
提交回复
热议问题