2017-05-18 19 views
0

有了一个名单,我通常将能够找到做这样一个元素:查找和优先级队列替换特定对象

int index = open.indexOf(object); 
if(index != -1){ 
    open.set(index, neighbour); 
} 

是否有替代/解决方法的优先级队列?

我:

PriorityQueue<State> open = createQ();

+0

如果您使用的是优先级队列有没有一种方法,让你用另一个替换的对象。您将拥有poll()所有对象,将它们添加到列表中,然后删除所需的对象,然后将它们添加()或提供()回到您的优先级队列中。确保事先调用contains()以确保要删除的对象存在于队列中 –

回答

0

你可以这样做:

PriorityQueue<String> pq = new PriorityQueue<>(); 
pq.add("test1"); 

// removes object and return true if it was removed 
if (pq.remove("test1")) { 
    // adds element in natural ordering 
    pq.add("test2"); 
}