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 Confused with Priority Queu's Behaviour 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 "Confused with Priority Queu Watch "Confused with Priority Queu New topic
Author

Confused with Priority Queu's Behaviour

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

As per my knowledge Priority Queue sort's on the bases of Natural Order.So if a Priority Queue is of String type then it will sort on the basis of a,b,c,d.....But just look at this code it doesn't agree....




Can someone please explain me why such behavior ...Or please correct me if ma concepts are wrong ......


OCJP-90%,OCPWCD-95%
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

Please Help me Guys.............
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

PatienceIsAVirtue.

You're not asking pq to return elements. You're asking it to print itself. Look at the documentation for toString().
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
Hello,
when you use System.out.prinln(pq) you are actually using pq.toString() method. PriorityQueue inherits this method from AbstractCollection class:
public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]").


And this is from PriorityQueue API:
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

Stoian Azarov wrote:Hello,
when you use System.out.prinln(pq) you are actually using pq.toString() method. PriorityQueue inherits this method from AbstractCollection class:
public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]").


And this is from PriorityQueue API:
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).




Thanks A Lot !!!.....Really Solved my mind's puzzle !!
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

Stephan van Hulst wrote:PatienceIsAVirtue.

You're not asking pq to return elements. You're asking it to print itself. Look at the documentation for toString().



yeah i know i should have been a little patient....but was really anxious to know the answer....Thanks a lot Stephan !!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
sagar shroff wrote:yeah i know i should have been a little patient....but was really anxious to know the answer.


A tip for those seeking instantaneous answers: Searching the forum may often yield results.

http://www.coderanch.com/t/267011/java-programmer-SCJP/certification/PriorityQueue
http://www.coderanch.com/t/270583/java-programmer-SCJP/certification/PriorityQueue
http://www.coderanch.com/forums/posts/list/473699#2121552
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with Priority Queu's Behaviour