我有是這樣創造了一個數據結構:重組模式匹配
// [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]]]]]]()
這樣做的原因是,我希望能夠看看這個映射了讓我的數據。
然而,在斯卡拉插入此數據結構我得到這樣的:
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)
}
這對我來說是一個代碼量瘋狂。我覺得這可能會縮短,但我沒有看到解決方案。
因爲我必須查找元素,如果它存在,那麼我可以很容易地插入元素。
但是,如果鏈中的元素不存在,那麼我必須創建元素,當然還有所有的子元素,就像你在上面的代碼中看到的那樣。
你有什麼想法,我怎麼能做到這一點更乾淨或可能使用更有組織的數據結構呢?
用於理解。如果你提供一些樣本數據,我可以寫一個例子。 –
嗯,我有我使用的MongoDB的一些輸出。 http://pastebin.com/12n6Tre7 – miniwolf