This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused with Priority Queu's Behaviour

 
Sagar Shroff
Ranch Hand
Posts: 209
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ......

 
Sagar Shroff
Ranch Hand
Posts: 209
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Help me Guys.............
 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 209
Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic