• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Priority Queue

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following method

void queues()
{
Comparator<String> c=new Comparator<String>(){public int compare(String a,String b){return b.compareTo(a);}};
Queue<String> qu=new PriorityQueue<String>(5,c);
String[] str={"The","Champ","Is","Here","Always"};
for(String s:str)
qu.offer(s);

System.out.println(qu);
System.out.println(qu.peek());
//System.out.println(qu.poll());
System.out.println(qu);
for(String as:qu)
System.out.println(as);


when this is compiled,i do not get a sorted queue.I am trying to get a sorting based on the reverse of natural order.
somebody please help

Thanks
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jitendra,

Unlike List classes, Queue classes do not guarantee that their iterators return the elements in any particular order. toString() and the enhanced for loop depend on the iterator order.

To get the correct sorted order, you need to repeatedly poll() the queue until it's empty.
 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kelvin

Unlike List classes, Queue classes do not guarantee that their iterators return the elements in any particular order.



I agree but PriorityQueue is supposed to be sorted.That means it should arrange elements in a sorted order(Higher priority first in this case).
Secondly,i have also implemented Comparator,This should have done the trick.
Don't you agree???
Please help!!!

Thanks
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct usage of PriorityQueue is poll(), peek(), offer(E element). You should not use PQ if you just want to iterate through it.

From PQ API description:


This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The 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()).

 
Jitendra Jha
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That really explains a lot.
K&B mentions that PQ is sorted,hence i tried this code.
Thanks a million
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic