• 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

PriorityQueue - why there is natural ordering ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused by PriorityQueue, why is this structure useful ? ...There is automatical ordering of elements .



I expected this :

but i got this :

i though that queue just return elements in the same order like elements were inserted !

Is there a 'queue' but without ordering ?

Tell me one useful example where is advantage to use PriorityQueue ...
Thank you
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

lucas van derstappen wrote:Is there a 'queue' but without ordering ?



Well a LinkedList is used for this purpose...
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

write by lucas
PriorityQueue pq = new PriorityQueue();

pq.offer("A");
pq.offer("C");
pq.offer("B");
pq.offer("X");
while (pq.size() > 0) {
System.out.print( pq.poll() + " ");
}



hi,
lucas
the output is a,b,c,x is correct. the PriorityQueue which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used.see natural order in the sense not the order which is specified in your program,the alphabet order
if you use this constructor you can get the output as you expected
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
i don't know in real time where this concepts are used.
hope you understand.


reply
    Bookmark Topic Watch Topic
  • New Topic