int compare(T o1, T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Sandra Bachan wrote:
According to API for Comparator, it says:
int compare(T o1, T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
I am unable to trace the logic of this code because I cannot grasp how Comparator works. Why does code in PQsort return two-one?
OCPJP 6.0 93%
OCPJWCD 5.0 98%
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Mohit G Gupta wrote:THe line 21-22 says :
can anyone explain how does the above code works?
Sandra Bachan wrote:
Cannot understand how PriorityQueue gets ordered using a Comparator.
According to API for Comparator, it says:
int compare(T o1, T o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Below is code based on Chapter 7, Sierra/Bates:
...
OCAJP 7, OCPJP 7
Nick Widelec wrote:
Just adding that oracle discourages the use of that technique "two-one" and they recommend to use "two.compareTo(one)" as if you inserted a negative value in your queue and, says, the Integer "two" has +3 as a value and the Integer "one" has -3 it will return 0 which the comparator sees it as meaning of equality, breaking also the "comparator consistent with "equals()" thing.
Henry Wong wrote:
Nick Widelec wrote:
Just adding that oracle discourages the use of that technique "two-one" and they recommend to use "two.compareTo(one)" as if you inserted a negative value in your queue and, says, the Integer "two" has +3 as a value and the Integer "one" has -3 it will return 0 which the comparator sees it as meaning of equality, breaking also the "comparator consistent with "equals()" thing.
If "two" has a value of +3, and "one" has a value of -3, doesn't "two-one" return a value of +6? ... ie. Doesn't positive 3 minus negative 3 equals six?
Henry
OCAJP 7, OCPJP 7
Katherine Reut wrote:
So MY question is, the PQsort comparator works as Chad and Henry explained, but I am still unsure about where the comparator actually arranges the elements into the sorted order. Is it on line 13 and 14, when the queue is loaded? But then in the comparator, what are the values of the first and second parameters? Where do they come from? Is the first parameter (Integer one) the value of "x" in the for-each loop, and the second parameter (Integer two) the value of the last element added to pq2?
Nick Widelec wrote:
Hi Henry,
You right, my bad, wrote it in a rush. I edited it now thanks for pointing it out.
Nick Widelec wrote:
Just adding that oracle discourages the use of that technique "two-one" and they recommend to use "two.compareTo(one)" as if you inserted a negative value in your queue and, says, the Integer "two" has -3 as a value and the Integer "one" has +3 it will return 0 which the comparator sees it as meaning of equality, breaking also the "comparator consistent with "equals()" thing.
However the book shows it in that way, maybe to simplify and to concentrate in the "comparator" interface.
Henry Wong wrote:
Nick Widelec wrote:
Hi Henry,
You right, my bad, wrote it in a rush. I edited it now thanks for pointing it out.
Nick, I am not sure of the point that you are trying to make -- as your edited response has a similar issue (just the sign has changed).
Nick Widelec wrote:
Just adding that oracle discourages the use of that technique "two-one" and they recommend to use "two.compareTo(one)" as if you inserted a negative value in your queue and, says, the Integer "two" has -3 as a value and the Integer "one" has +3 it will return 0 which the comparator sees it as meaning of equality, breaking also the "comparator consistent with "equals()" thing.
However the book shows it in that way, maybe to simplify and to concentrate in the "comparator" interface.
If "two" has a value of -3, and "one" has a value of +3, doesn't "two-one" return a value of -6? ... ie. Doesn't negative 3 minus positive 3 equals negative six?
Henry
OCAJP 7, OCPJP 7