File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding java.lang.Comparator" Watch "Regarding java.lang.Comparator" New topic
Author

Regarding java.lang.Comparator

Krishna Potluri
Greenhorn

Joined: Apr 20, 2007
Posts: 19
Hi,
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:
*********************************************************************
5
7
6
10
8
9

**********************************************************************

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,
Kris


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;

}
else{

return -1;
}

}

}
public class PriorityQueueTest {

public static void main(String[] args) {
PriorityQueue priorityqueue = new PriorityQueue(6,new Ascending());
priorityqueue.offer("10");
priorityqueue.offer("9");
priorityqueue.offer("8");
priorityqueue.offer("7");
priorityqueue.offer("6");
priorityqueue.offer("5");

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

for(Object o : priorityqueue){

category.info(o);
}

}

}
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é
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

API:
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.


Regards,
cmbhatt


cmbhatt
Krishna Potluri
Greenhorn

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

for(Object o riorityQueue){
category.info(o);
}

WITH

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

Thanks and Regards,
Krishna
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: http://aspose.com/file-tools
 
subject: Regarding java.lang.Comparator