[英]Remove from SortedMap in scala
我有一些這樣的地圖
val m = SortedMap[Long, String]()
val buffer = 1000
和方法
def m(l: Long, s: String) = {
m + { (l, s) }
//Now in order to avoid OutOfMemory
//I want to keep in the m keys in the interval
//[max - buffer, max]
}
如何從映射中刪除[max - buffer, max]
以外的所有值。
from
的方法不是我想要的,因為它創建了一個投影並且條目符合垃圾收集的條件。
最后N個元素
如果您只想保留最右邊的N個元素,則由於您的鍵已排序,因此SortedMap#takeRight
將起作用:
val updatedM = m takeRight buffer
范圍內
如果要將密鑰保持在一定范圍內,那么--
似乎就是您想要的:
val goodKeys = (max - buffer) to max
val updatedM = m -- (m.keys -- goodKeys) //*
*根據TheArchetypalPaul的准確評論進行了更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.