# another question from scjptest.com

Ankur kothari
Ranch Hand
Posts: 531

how is the output 1 2 2....what does poll do? i think it gives the highest priority entry...how is 1 printed first...?

Ankit Garg
Sheriff
Posts: 9521
22
If you have three strings "1", "2" and "3" in an array, and if you sort it, then which element would come first?? The poll method returns the first element of the queue
javadocs wrote:public E poll()
Retrieves and removes the head of this queue

Ankur kothari
Ranch Hand
Posts: 531
how are 1 2 3 strings compared? ok on page 592 of k&B why is 9 printed? is it because it the highest? or because it is thehead?

Ankit Garg
Sheriff
Posts: 9521
22
Strings are compared lexicographically (by default, you can change it using Comparator).
Ankur kothari wrote: ok on page 592 of k&B why is 9 printed? is it because it the highest? or because it is thehead?

I don't have the book at hand. Can you post the question...

Ankur kothari
Ranch Hand
Posts: 531
....there was a for loop and poll was called through the queue.....and it had printed 1 2 3 4 5 6 7 8.......inspite of being added as 1 3 2....different order....

what is natural ordering? that is , the book said an entry of 1 will be a higher priority than 2?

Ankur kothari
Ranch Hand
Posts: 531
when we add 1 2 or numbers in the priorityqueue do they get arranged in thier ascending order(natural order)...i think yes....but please confirm

Ankur kothari
Ranch Hand
Posts: 531
and does the alphabetic spelling order mean 1 2 3 will be sorted acccording to one two and three...that is one three and two?

Ankit Garg
Sheriff
Posts: 9521
22
Ankur kothari wrote:and does the alphabetic spelling order mean 1 2 3 will be sorted acccording to one two and three...that is one three and two?

Why would that happen?? "1" itself is a string, it won't be converted to "one". PriorityQueue is like a sorted queue. By natural order, the book means the order decided by calling the compareTo method on the elements (or compare method if Comparator is used)...

Ankur kothari
Ranch Hand
Posts: 531

this prints me 1 3 2 -1.......why 1 3 2?

Ankit Garg
Sheriff
Posts: 9521
22
Its all in the documentation, PriorityQueue inherits toString method from AbstractCollection class. The documentation of toString method of AbstractCollection class says
docs of AbstractCollection.toString wrote:Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

So basically toString uses iterator method of PriorityQueue to generate the output. The iterator method of PriorityQueue class says
docs of PriorityQueue.iterator wrote:Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

rohan yadav
Ranch Hand
Posts: 156
Ankit Garg wrote:Its all in the documentation, PriorityQueue inherits toString method from AbstractCollection class. The documentation of toString method of AbstractCollection class says
docs of AbstractCollection.toString wrote:Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space).

So basically toString uses iterator method of PriorityQueue to generate the output. The iterator method of PriorityQueue class says
docs of PriorityQueue.iterator wrote:Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Why the output is [1,3,2].
PriorityQueue uses natural ordering or can be ordered using Comparators.
As no comparator is used in above example , isn't queue should be sorted by natural ordering??

I mean output should be [1,2,3] but it is giving [1,3,2]!!!

Why??

Ankur kothari
Ranch Hand
Posts: 531
even i have the same question.....when we put integers 1 4 2 they get arranged in ascending order...if we put strings wi ww wo...they get get arranged in ascending order too.....but if we out strings "1" "3" "2" the order is absurd......and when we call poll the order is different

Ankur kothari
Ranch Hand
Posts: 531
by trying it out..i have discovered that we cant predict the order of them inside it...but we can predict the order when poll is called....

Ankur kothari
Ranch Hand
Posts: 531
when called poll...the least of them is called first....when comparing strings 1 2 3 .....they are compared in their int values....1<2<3

Ankur kothari
Ranch Hand
Posts: 531
nope i am wrong...when ints are represented in strings...poll doesnt work the same way as ints....

Ankur kothari
Ranch Hand
Posts: 531

try it out

Ankur kothari
Ranch Hand
Posts: 531
strings are compared letter by letter...that is char by char....so since 1<2<3..first 100 is polled out..then 2..then 301

rohan yadav
Ranch Hand
Posts: 156
Oh i got it, Its because queue's toSting() method uses iterator to traverse queue, But the order in which queue is traversed can't be determined.

Ankur kothari
Ranch Hand
Posts: 531
i didnt undertand a word what you said...but glad we both understood

rohan yadav
Ranch Hand
Posts: 156
Ankur kothari wrote:i didnt undertand a word what you said...but glad we both understood

Yes dude that's the main point

Ankit Garg
Sheriff
Posts: 9521
22
I'm not sure what you two understood or not.
Ankur kothari wrote:even i have the same question.....when we put integers 1 4 2 they get arranged in ascending order...if we put strings wi ww wo...they get get arranged in ascending order too.....but if we out strings "1" "3" "2" the order is absurd......and when we call poll the order is different

As I quoted from the javadoc, if you print the elements of a PriorityQueue, then the order of elements is not guaranteed.
when ints are represented in strings...poll doesnt work the same way as ints....

This has nothing to do with behavior of poll. the poll method behaves exactly the same whatever the content of the queue is. Do one thing, create files with the name "1.txt", "2.txt", "301.txt", "100.txt" in your windows and arrange them by name. You won't get the output as you expect. You need to understand what Lexicographic sorting is. Strings are sorted Lexicographically, so "2" not necessarily comes before "100"...

Ankur kothari
Ranch Hand
Posts: 531
i know Ankit....100 will be polled before 2.....because 1<2......

Ankur kothari
Ranch Hand
Posts: 531
and as you said.....the order of the queue will be unpredictable...this is what i meant