如果需要根据插入顺序进行排序,则需要使用额外的元素作为时间戳。即在插入和相等的权重下使用,
timestamp以查看首先插入哪个元素。所以
CustomObject应该是这样的:
class CustomObject { int weight; long timestamp; }比较应该是:
public int compareTo (CustomObject o) { int thisWeight = this.weight; int thatWeight = o.weight; if (thisWeight != thatWeight) { return thisWeight - thatWeight; } else { return this.timestamp - o.timestamp; } }较小的
timestamp表示它是 较早 插入的 , 因此您可以保持插入顺序。
您还可以通过维护在每个
add或上更新的计数器来使用“逻辑”时间
remove。



