我正在使用scala.collection.mutable.TreeSet,并遇到一个问题,在调用-=
时无法删除元素。scala treeset无法删除元素
我的代码:
val discovered = new TreeSet[Position]()(Ordering by { position => estimation(position) })
//Position is defined as: type Position = (Int, Int)
discovered += start
var x = 0
while(!discovered.isEmpty){
val current = discovered.head
println(discovered)
discovered -= current
println(discovered)
x += 1
println(s"$x $current")
[...] //Code to process current and discover new positions
}
以下示例显示了,即(18.46)不会被删除。直到那一刻,清除工作完美。我还有其他的测试用例,这些测试用例可以完美地工作,而其他情况下,只要达到大约100次迭代,这个问题就不会发生。我已经得到了与TreeSet
的不变实现相同的结果。输出
部分:
TreeSet((22,42), (18,46), (21,44), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47))
TreeSet((18,46), (21,44), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47))
14 (22,42)
TreeSet((18,46), (21,44), (22,41), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47))
TreeSet((18,46), (21,44), (22,41), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47))
15 (18,46)
TreeSet((18,46), (21,44), (22,41), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47), (17,46))
TreeSet((18,46), (21,44), (22,41), (24,46), (22,47), (21,43), (21,47), (23,47), (24,47), (17,46))
16 (18,46)
如何定义估计(位置)?也许排序不健壮? – Suma
'val estimation = new HashMap [Position,Float] .withDefaultValue(Float.PositiveInfinity)' 当估计值发生变化时,总是更新'discovered': 'estimate(somePosition)= newScore; if(alreadyDiscovered){discovered - = somePosition} discover + = somePosition' – TheJP
这是一个可变的散列表吗?是否有任何值填充它? – Suma