• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Regarding java.lang.Comparator

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}

}
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic