我正在使用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