File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes comparator-someone answer this 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 "comparator-someone answer this" Watch "comparator-someone answer this" New topic

comparator-someone answer this

shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42

This code produces something like this:
1 3 5 6 7 8 9
size 7
peek 9
size 7
poll 9
size 6
8 7 6 5 3 1 null

i dont understand the comparator part of it.....
a) wat does this code do:


how does this do a inverse sort.....what is taken in as integer one and integer two.......i understand that it takes in the given array....but hoe does it do they get it as 9 8 7 6 5 3 1....can anyone explain me clearly.....

nothing is goin in to my head!!!(have got my exam this month ).can anyone gimme a link where the minute tips and traps for the exam should be read......

Thanks in advance

[ August 16, 2006: Message edited by: shilpa Reddy ]
[ August 16, 2006: Message edited by: shilpa Reddy ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you wish that it is more likely for people to read your code, then please place correctly formatted code between tags.

You can edit your original post by selecting the edit icon (the one like a piece of notepaper and pencil)

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Stephen O'Kane

Joined: Aug 17, 2005
Posts: 26
It's all in the API if you care to look at it:

PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

The static class PQsort will take two Integer objects and compare them, not according to natural order (which is default for a PriorityQueue), but to reverse natural order (hence return two - one; if it was return one - two; then it would still be natural order).

When a PriorityQueue is created with a Comparator, then it will use the comparator to decide where in the Queue the value should go. If it is created with no Comparator, it uses natural order
shilpa Reddy
Ranch Hand

Joined: Jul 26, 2006
Posts: 42
Thanks a lot for tha here the initial capacity is 10... wat is the significance of it......does it mean that the priority queue will contain 10 elements......i understand that it is reverse sorted order.....but i dont understand how it happens....can you explain that basic thing...wat would go in as the Interger 1 and Integer do they compare and how does return two-one give you 9 8 7......can you please explain.....

[ August 16, 2006: Message edited by: shilpa Reddy ]
[ August 16, 2006: Message edited by: shilpa Reddy ]
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144

Please can someone explain how the below code works and inverse sorting happens:

How are the 2 parameters Integer one and Integer two are passed to the compare method and how the difference between the 2 parameters does the sorting..I am really confused.. Please help

Thanks in advance
Charmy Madhvani
Ranch Hand

Joined: Dec 26, 2007
Posts: 59
Hi Moieen and Shilpa,
According to what I have understood from API.

When we give a comparator type instance To priority queue constructor, its elements are sorted in the order in which the compare method is implemented. Here when we are calling constructor of pq2 , we are providing it with pqs. The compare method implemented here works like this.
Lets say we have given the queue the elements 1 and then 5
The compare method will execute for lets say(1,5) and return o/p >0
if o/p>0 then one is greater than two
if o/p<0 then one is lesser that two
if o/p==0 then one is equal to two.

and will place elements in order 5,1
Then we add 3 (5,1,3)

Then it will execute for (1,3) and return o/p >0
and will place elements in order 5,3,1

and so on.. If you go on like this way for each combination you will definitely get a reverse sort of entire elements.
I assume this is done internally within priority queue.
When and where even I dont know.(May be while loading elements)..

I dont know whether this is a correct explanation.
But this is what I have concluded..

SCJP 5.0<br />SCWCD 5.0
I agree. Here's the link:
subject: comparator-someone answer this
It's not a secret anymore!