File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding java.lang.Comparator

 
Krishna Potluri
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Krishna Potluri
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic