Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Doubt in PriorityQueue

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my own code
could some one explain how the code run and what does "A PriorityQueue's elements are ordered either by natural ordering (in which case the elements that are sorted first will be accessed first) or according to a Comparator" mean?
import java.util.*;
public class TestQuence
{
public static void main ( String [] args )
{
Queue q = new PriorityQueue();
q.add( new Integer(3));
q.add(new Integer(1) );
q.add(new Integer(2));

for ( Object o : q )
{
System.out.println(o+ " " + q.size());
}
}
}
And the o/p is:
1 3
3 3
2 3

Thank you Very much..
Sun Craven
[ April 16, 2008: Message edited by: Sun Craven ]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sun,

For your first doubt

A PriorityQueue's elements are ordered either by natural ordering (in which case the elements that are sorted first will be accessed first) or according to a Comparator


Priority queue stores elements in a sorted order. This ordering could be in one of the two forms:-
1.Natural ordering
2.Using Comparator(ie Implementing the comparator interface and having our own custom sort.)

For your second doubt on how the code compiles:-
For priorityQueues we use the following three methods:-
1.offer()-To add elements to the queue
2.peek()-To get the highest priority element
3.poll()-To get the highest priority element and remove it from the queue.
A modified version of your code is given below:-


Hope this helps
 
Sun Craven
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajat:
i have got you.Thank you again.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic