• 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

Collections..

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;

public class PQ {

static class PQsort implements Comparator<Integer>
{
public int compare(Integer one, Integer two)
{
return two.compareTo(one);


}
}

public static void main(String[] args)
{
int [] ia = {1, 5 ,3 , 7 ,6, 9 ,8,};
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>() ;
for (int x : ia)
pq1.offer(x);
for (int x : ia)
System.out.print(pq1.poll() + " ");

System.out.println(" ");

PQsort pqs = new PQsort();
PriorityQueue <Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

for (int x : ia)
pq2.offer(x);
System.out.println("size " + pq2.size());
System.out.println("peek " + pq2.peek());
System.out.println("size " + pq2.size());
System.out.println("poll " + pq2.poll());
System.out.println("size " + pq2.size());


for (int x : ia)
System.out.println(pq2.poll() + " ");
}

}

// i have not understood this program can somebody expalin it to me line by line... thanks..
[ May 23, 2008: Message edited by: sweety singh ]
 
Greenhorn
Posts: 6
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a sample code for PriorityQueue implementation.
"An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException)"

pq.offer(E e) inserts the element
pq.peek() gives the last inserted element
pq.poll() takes out the last inserted element
pq.size() gives you the number of elements in the queue.

as simple as that ....

the program initially puts the array elements into the queue (in the natural order)and the prints it

the next constructor gives in a comparator to define the ordering (reverse in this case).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"zzyborg", please check your private messages. You can see them by clicking My Private Messages.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which part of the program you can't understand?inner class or Generics or methods of class PriorityQueue ?
[ May 23, 2008: Message edited by: Ulf Dittmer ]
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sweety singh:
import java.util.*;

public class PQ {

static class PQsort implements Comparator<Integer>
{
public int compare(Integer one, Integer two)
{
return two.compareTo(one);


}
}

public static void main(String[] args)
{
int [] ia = {1, 5 ,3 , 7 ,6, 9 ,8,};
PriorityQueue<Integer> pq1 = new PriorityQueue<Integer>() ;
for (int x : ia)
pq1.offer(x);
for (int x : ia)
System.out.print(pq1.poll() + " ");// 1

System.out.println(" ");

PQsort pqs = new PQsort();
PriorityQueue <Integer> pq2 = new PriorityQueue<Integer>(10,pqs);

for (int x : ia)
pq2.offer(x);
System.out.println("size " + pq2.size());
System.out.println("peek " + pq2.peek());
System.out.println("size " + pq2.size());
System.out.println("poll " + pq2.poll());
System.out.println("size " + pq2.size());


for (int x : ia)
System.out.println(pq2.poll() + " ");
}

}

// i have not understood this program can somebody expalin it to me line by line... thanks..



what is happening in line 1... poll() will remove the priority element.. but it displays all...???..
[ May 23, 2008: Message edited by: sweety singh ]
 
sweety singh
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i still havent understood the same line...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The poll() call happens in a loop, so this line successively removes all elements, and prints each one as it does so.
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sweety singh:
i still havent understood the same line...






This is the API of poll method.

when you invoke it like this: pq1.poll(),it first "Retrieves and removes the head of this queue",so "poll() will remove the priority element"

second,it " Returns:
the head of this queue,"

so it returns the element which it just removes

then when you code


JVM prints the element it returns
 
reply
    Bookmark Topic Watch Topic
  • New Topic