我有一些这样的有序地图
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
不是我想要的,因为它创建了一个投影,并且条目可以用于垃圾收集。
答案 0 :(得分:2)
最后N个元素
如果你只想保留最正确的N个元素,那么SortedMap#takeRight
将起作用,因为你的密钥是有序的:
val updatedM = m takeRight buffer
在范围内
如果您想将密钥保持在一定范围内,那么--
似乎就是您想要的:
val goodKeys = (max - buffer) to max
val updatedM = m -- (m.keys -- goodKeys) //*
*根据TheArchetypalPaul的准确评论更新。