File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question regarding PriorityQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question regarding PriorityQueue" Watch "question regarding PriorityQueue" New topic
Author

question regarding PriorityQueue

Dinuka Arsakularatne
Ranch Hand

Joined: Aug 12, 2006
Posts: 198
i got this question from the SCJP 5 book. can some one please explain to me why the value 10 has been put in the line of the code which i have commmented out below. and how does the comapre method check the Integers to determine that they must go in reverse order? i just do not understand the logic behind the compare method. please help

import java.util.*;

class pq {
static class pqsort implements Comparator<Integer>{
public int compare(Integer one, Integer two){
return two - one;

}

}

public static void main(String ar[]){

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());
}
}
thank you
Dinuka Arseculeratn


if who i am is what i have, and what i have is lost, then who am i?<br /> <br />SCJP 5.0<br />SCWCD 1.4<br />SCBCD preparing
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dinuka Arseculeratne:
...can some one please explain to me why the value 10 has been put in the line of the code which i have commmented out below. and how does the comapre method check the Integers to determine that they must go in reverse order? ...

The API documentation for Comparator's compare method says it returns "a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second."

In this example, compare(Integer one, Integer two) simply returns two - one. Therefore, if one has a smaller value than two, the method will return a positive result indicating that one is "greater than" two. In other words, the smaller value will be ordered after the larger value.

The 10 specified in the constructor is just an initial capacity for this PriorityQueue. (See API documentation.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
maybe if you had a look around the forum (hint, search function near the top of the page), you might find another topic discussing the exact same problem, i.e.

http://www.coderanch.com/t/258350/java-programmer-SCJP/certification/comparator-someone-answer

oh, and feel free to try and format your code as well. It makes it much easier for anyone trying to help you. You help us, we help you.

Sok
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

(Instructions for using Code Tags are here.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question regarding PriorityQueue
 
Similar Threads
Priority Queue
Priority queue
Collections..
question regarding PriorityQueue
priority queue!