Why am I getting the following result?

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.

+4
source share
4 answers

The order is guaranteed upon access to the head with a survey, deletion, viewing or taking, for example, but not upon repetition:

The iterator provided in the iterator method () is not guaranteed to intersect the elements of the priority queue in any particular order.

This will give the expected result:

 String s; while ((s = pq.poll()) != null) { System.out.println(s); } 

outputs:

 Sing Sing10 Sing11 Sing12 Sing13 Sing2 Sing3 Sing4 

What is the purpose of creating a constructor with the parameter "initial capacity", which actually does not set the boundary.

The initial capacity is not a boundary. The queue is supported by the array - by setting the initial capacity, you can avoid unnecessarily resizing the array (similar to the ArrayList constructor).

+10
source

The javadoc says:

The iterator provided in the iterator () method does not guarantee that PriorityBlockingQueue elements will pass in any particular order. If you need an ordered workaround, consider using the Arrays.sort array (pq.toArray ()).

When using peek()/poll()/take() you will get the elements in order. But not with repetition.

To answer your second question: the queue is implemented as an array of objects inside. Setting the initial capacity allows you to avoid too many copies of arrays when the queue grows and the number of elements exceeds the capacity. Just like ArrayList.

+5
source

The initial capacity is the size at which the container in which your items are stored is initialized with @ -). As you add items, capacity grows automatically. It depends on the growth policy (determined by the base container), what are the costs of expanding the size of the container.

PriorityBlockingQueue - An unlimited blocking queue by definition . The goal of creating such a constructor with the "initial capacity" parameter is to initialize the base container with a value that makes sense for the actual use case. This cannot be provided by the developer of PriorityBlockingQueue .

LinkedBlockingQueue is an optionally limited blocking queue by definition . Thus, the parameter has - if set - the function to act as an upper bound.

+1
source

Mark the link

The iterator provided in the iterator () method is not guaranteed to cross PriorityBlockingQueue elements in any particular order. If you need an ordered workaround, consider using Arrays.sort (pq.toArray ()). In addition, the drainTo method can be used to remove some or all of the elements in order of priority and put them in another collection.

0
source

Source: https://habr.com/ru/post/1492106/


All Articles