A LinkedList
is the worst choice. Either use an ArrayList
(or, more generally, a RandomAccess
implementor), or PriorityQueue
. If you do use a list, sort it only before iterating over its contents, not after every insert.
One thing to note is that the PriorityQueue
iterator does not provide the elements in order; you'll actually have to remove the elements (empty the queue) to iterate over its elements in order.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…