This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How does a PriorityQueue work ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does a PriorityQueue work ?" Watch "How does a PriorityQueue work ?" New topic

How does a PriorityQueue work ?

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339

I've been having trouble learning how a PriorityQueue works..

Do you know if a PriorityQueue sorts elements when you add them ?

I'm asking because the behavior of this piece of code here is difficult to understand... (for me)

That code prints out:

Amazing : Fantastic

But If I change the line with the comment "//this line" for this line:

it prints out:

Fantastic : Outstanding

Which leads me into thinking that some sorting happens backstage.

Could somebody please tell me, How does a PriorityQueue work?

I'm assuming that If Sorting happens, the poll() method removes the last element in the PriorityQueue after sorting by natural order...

Am I correct?

please help me,

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Jose,

A priority queue could work by sorting after each insertion, but it would be slow; each insertion would take time proportional to N*ln(N), where N is the size of the queue. But if you think about it, there's no reason for all the items in the queue to be sorted: you just need to know, at all times, which is the "first" item.

So instead, most priority queues use a data structure called a heap, which is a kind of partially-sorted binary tree. The tree is arranged so that every child node "below" a parent node in the tree would come after the parent if all the nodes were sorted; but the children of a parent aren't necessarily next if the tree were sorted. If you think about it, though, this means that the very first node in the sort is always at the root of the tree, and things are partially sorted as you move down the tree.

It turns out that after removing the root or adding a new child, you can restore the tree to a proper heap in ln(N) time, which is quite fast. This makes a heap an ideal data structure for implementing a priority queue.

[Jess in Action][AskingGoodQuestions]
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
Hello again,

Complex, really complex I must say. So, Could I assume that there's No way I could know how objects are arranged inside my PriorityQueue ? with this I mean an Easy way...?

I'm really worried I don't wanna be stuck with a question like that on the SCJP exam.

Thank you for the insights Ernest!
They're always welcome.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You definitely don't need to understand how a PriorityQueue works internally for the SCJP exam. You might need to know that if you call toString() or if you iterate through the contents of the Queue, the order you get is not necessarily sorted by priority. Much like with a HashMap, if you iterate or call toString(), you can think of the results as being in random order. They're not really random, but the order is complicated and you don't really need to worry about it. Unless you're studying algorithms for, say, an algorithms class.

"I'm not back." - Bill Harding, Twister
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

and just to be clear, the "heap" data structure is different from the "heap" memory pool. they are two separate concepts with the same name.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339

I'm grateful for your responses, It's so cool to know more about the details of the language, I'll have to leave the internal algorithms for later though.
And, I'm so relieved that questions about PriorityQueues in the SCJP exam, aren't as complicated as I imagined.

Have a nice day !
Keep up the fantastic work,
I agree. Here's the link:
subject: How does a PriorityQueue work ?
It's not a secret anymore!