aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Using the PriorityQueue Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Using the PriorityQueue Class" Watch "Doubt in Using the PriorityQueue Class" New topic
Author

Doubt in Using the PriorityQueue Class

archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66



In the declaration " PriorityQueue<Integer> pq2 =
new PriorityQueue<Integer>(10,pqs) "

i'm unable to understand as why 10 is added ???javascript:emoticon('');

This code was taken from kathy sierra 1.5 scjp....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Did you check the Javadocs for PriorityQueue? They explain what the constructor arguments mean.
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

ya i have checked....Help me out please...javascript:emoticon('');
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

The Javadocs tell you that the 10 sets the initial capacity of the queue. They also say
A priority queue is unbounded, but has an internal capacity governing the size of an array used to store the elements on the queue. It is always at least as large as the queue size. As elements are added to a priority queue, its capacity grows automatically. The details of the growth policy are not specified.

When you add items that take the queue beyond the capacity, it has to increase the size of the internal array. This takes a bit of time. So if you know how big the queue has to get you can set the capacity to be big enough in the first place, which saves this time.
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

@ Matthew Brown :


Thanks a ton!!
 
 
subject: Doubt in Using the PriorityQueue Class