重组模式匹配

时间:2015-01-16 09:00:02

标签: scala dictionary data-structures pattern-matching

我有一个如下所示的数据结构:

// [Measure -> [Installation -> [Year -> [Month -> [Day -> [Hour -> BigDecimal] ]]]]]
private var dayList = new mutable.HashMap[String, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]]]()

原因是我希望能够查看此映射以获取我的数据。

但是,在scala中插入这个数据结构我会得到这样的结果:

    dayList.get(col) match {
        case Some(measureLook) =>
          measureLook.get(installation) match {
            case Some(instaLook) =>
              instaLook.get(tempYear) match {
                case Some(yearLook) =>
                  yearLook.get(tempMonth) match {
                    case Some(monthLook) =>
                      monthLook.put(tempDay, hourCounter)
                    case None =>
                      val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
                      m.put(tempDay, hourCounter)
                      yearLook.put(tempMonth, m)
                  }
                case None =>
                  val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
                  val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
                  m.put(tempDay, hourCounter)
                  y.put(tempMonth, m)
                  instaLook.put(tempYear, y)
              }
            case None =>
              val in = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]()
              val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
              val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
              m.put(tempDay, hourCounter)
              y.put(tempMonth, m)
              in.put(tempYear, y)
              measureLook.put(installation, in)
          }
        case None =>
          val me = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]]()
          val in = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]]()
          val y = new mutable.HashMap[Int, mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]]()
          val m = new mutable.HashMap[Int, mutable.HashMap[Int, BigDecimal]]()
          m.put(tempDay, hourCounter)
          y.put(tempMonth, m)
          in.put(tempYear, y)
          me.put(installation, in)
          dayList.put(col, me)
      }

对我来说,这是疯狂的代码量。而且我觉得这可能会以某种方式缩短,但我没有看到解决方案。

因为我必须查找元素,如果它存在,那么我可以很容易地插入元素。

但是如果链中某处的元素不存在,那么我必须创建元素,当然还要创建所有子元素,如上面的代码所示。

您是否有任何想法,我可以做得更清洁,或者为此使用更有条理的数据结构?

1 个答案:

答案 0 :(得分:2)

考虑为每个

定义一个案例类
[Measure -> [Installation -> [Year -> [Month -> [Day -> [Hour -> BigDecimal]]]]]]

即例如,

case class Data(Measure: String,
                Installation: String
                date: java.util.Date,
                n: BigDecimal)

将所有Array[Data]全部收集到感兴趣的字段{<1}}上

<强>更新

MapMeasure

的索引编制的描述
Installation

注意val info[Array[Data]] = Array(data_1,..., data_n) val meInsIdx = info.map ( d => (d.Measure,d.Installation) -> d ).toMap 键必须是唯一的。要在Map中查询data_i,请考虑

meInsIdx

提供val data_i = meInsIdx.get(measure_i, installation_i) Option[Data]Some(data_i)以获取不存在的密钥。