This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding java.lang.Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding java.lang.Comparator" Watch "Regarding java.lang.Comparator" New topic

Regarding java.lang.Comparator

Krishna Potluri

Joined: Apr 20, 2007
Posts: 19
I wrote a program which uses PriorityQueue. The arguments to the PriorityQueue is as follows:

PriorityQueue priorityqueue = new PriorityQueue(6,new Ascending());

Ascending is a class which implements Comparator interface.

This program should print numbers in ascending order like:
5 6 7 8 9 10

but it is showing output as follows:


Kindly clear my doubt?

Why it is not printing the proper output. Acoording to Comparator logic it should give output , in ascending order.

Thanks in advance,

Actual source code as follows:

package com.kris.tigerv;

import java.util.PriorityQueue;
import java.util.Comparator;
import org.apache.log4j.Category;

class Ascending implements Comparator{
public int compare(Object o1,Object o2){
int i = new Integer(o1.toString()).intValue();
int j = new Integer(o2.toString()).intValue();
if(i == j){

return 0;
else if(i > j){
return 1;


return -1;


public class PriorityQueueTest {

public static void main(String[] args) {
PriorityQueue priorityqueue = new PriorityQueue(6,new Ascending());

//Category is Log4j class.
Category category = Category.getInstance(PriorityQueueTest.class);

for(Object o : priorityqueue){;


bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
I'm not very familiar with PriorityQueue, but looking at the javadoc
it says "The iterator does not return the elements in any particular order."

This might be your confusion.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Try this instead :

[My Blog]
All roads lead to JavaRanch
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Krishna,

I think you should stick to to your code. And while getting items from the
PriorityQueue you use poll() method, because iterator does not guarantee any
specific behavior. Use peek method to get the top item (greatest number in the
queue) and poll() to remove that as well.
Arrays.sort() is not required in your case as I think, if you follow the peek() or poll() way.


Krishna Potluri

Joined: Apr 20, 2007
Posts: 19
Hi Satou ,
Above source code displaying the proper output i.e. 5 6 7 8 9 10 after

for(Object o riorityQueue){;


Object o = null;
while ( (o = priorityqueue.poll()) != null ) {;}
Thanks for every one who tried to cleared my doubt.

Thanks and Regards,
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
One additional remark, you are sure you need the queue interface (peek() poll()...)?
If you just want a sorted collection and want to iterate over it, you might consider using something like a TreeSet, which is also sorted and its iterator iterates as you would expect.
I agree. Here's the link:
subject: Regarding java.lang.Comparator