priority queue - Java: PriorityQueue returning incorrect ordering from custom comparator? -


This question is already an answer here

  • 4 replies

I created a custom The comparator is written to compare my node classes, but the Java priority queue is not returning its items in the correct order

Here is my comparator:.

  public integer comparison (node ​​n1, node n2) {if (n1.getF ()> gt; n2.getF ()) {back 1; } And if (n1.getF () & lt; n2.getF ()) {return -1; } Else {// equal return 0; }}  

Where getF returns a double, though after inserting several nodes in the priority queue, I print them out using:

  while open .size ()> 0) {node t = (node) (open.remove ()); Println (t.getF ()); }  

Which results in:

  6,830951894845301 6,830951894845301 6,0 6,0 5,242640687119285 74031242374328485 74031242374328485 8 , 071067811865476  

Why is any idea why? Is my comparator wrong? Thank you.

Mike

How are you printing those values? I do not think that the Iterator PriorityQueue from provides the same order assurance composite class, that is so potential if you are providing

 for  (node ​​n: Line) {System.out.println (n.getF ()); }  

You will get unordered output. I order assurance is possibly some other methods, proposal , take , choice , peeking , and

There is a special mention on iterator in the javadocs for priority queue


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -