posted 11 years ago
So I was playing with priority queue. Here is the code (its loosely based on K&B example).
The output is:
Queue created with Descending priority
----------------------------------------
Highest priority: [Name:10]
Iterator
[Name:10][Name:9][Name:7][Name:5][Name:2][Name:3][Name:4][Name:1]
Poll
[Name:10][Name:9][Name:7][Name:5][Name:4][Name:3][Name:2][Name:1]
Queue created with Ascending priority comparator
--------------------------------------------------
Highest priority: [Name:1]
Iterator
[Name:1][Name:2][Name:3][Name:7][Name:5][Name:9][Name:4][Name:10]
Poll
[Name:1][Name:2][Name:3][Name:4][Name:5][Name:7][Name:9][Name:10]
My Question is:
When I use iterator to print values it does not print in order of priority, but when I poll() it follows the priority order.
Why? Can someone please explain this behavior? Iterator does not use comparable or comparator interface?
Thanks.