4

how to update the order of java priority queue after changing its elements value.

i need it in path choosing where the child path cost depend on the current one

Queue<String> lowerCostPath = new PriorityQueue<>();
    lowerCostPath.add(path1);
    lowerCostPath.add(path2);
    lowerCostPath.add(path3);
    lowerCostPath.add(path4);
    lowerCostPath.add(path5);
    while(!lowerCostPath.isEmpty()){
        String current = lowerCostPath.poll();
        if(pathType(current)=="water"){
            lowerTheCost(current);
            String[] changed = updateRelatedPathes(current);
            for(String s : changed){
                //lowerCostPath.changed(s); //how to inform queue about change
            }
        }
    }

i searched the answers and one of them suggest to use observer pattern to inform the priority queue that a change occurred and it will update the order of its elements,

is it possible, if so how to do it, if not what was he meaning.

this is the link to the answer https://stackoverflow.com/a/715050/5418289

Community
  • 1
  • 1
Ahmed Mazher
  • 323
  • 3
  • 7
  • 1
    Based on the other answers to that question, I suspect the answer you've linked to is assuming a custom PriorityQueue class; that is, if you implement your own PriorityQueue, you might use the Observer pattern to signal changes in priority. – jaco0646 Oct 17 '15 at 22:39

0 Answers0