aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections.." Watch "Collections.." New topic
Author

Collections..

sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
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 ]
sherin stephen
Greenhorn

Joined: May 14, 2006
Posts: 6

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).
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

"zzyborg", please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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 ]

SCJP 5.0 72%
sweety singh
Ranch Hand

Joined: Dec 15, 2007
Posts: 49
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

Joined: Dec 15, 2007
Posts: 49
i still havent understood the same line...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42266
    
  64
The poll() call happens in a loop, so this line successively removes all elements, and prints each one as it does so.


Ping & DNS - my free Android networking tools app
Gary Kevin
Ranch Hand

Joined: Jul 24, 2006
Posts: 43
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collections..