I did the following test to see how
PriorityBlockingQueue<String> pq = new PriorityBlockingQueue<>(2); pq.put("Sing"); pq.put("Sing2"); pq.put("Sing3"); pq.put("Sing4"); pq.put("Sing10"); pq.put("Sing11"); pq.put("Sing12"); pq.put("Sing13"); for (String s1: pq) { System.out.print(s1 +" "); }
The result is:
Sing Sing10 Sing11 Sing13 Sing2 Sing3 Sing12 Sing4
Now, as the API says, it should order them in kind if no comparator is specified during construction. Hoever, as you can see that the result is not ordered at all.
Secondly, the initial capacity I was equal to 2, why is there such an option if the border is not really set? What's the point? I understand that api indicates that it is an unlimited priority queue, but why even create an initial capacity if it cannot set any of its boundaries?
So basically I have 2 questions:
1) Why does the order of the above result not correspond to the natural order of the elements?
2) . What is the purpose of creating a constructor with the parameter "initial capacity", which actually does not set the boundary. In LinkedBlockingQueue, it is reasonable as it sets a limit, however this does not happen in PriorityBlockingQueue.
Thanks in advance.
source share