• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Confused with Priority Queu's Behaviour

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please Help me Guys.............
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

https://coderanch.com/t/267011/java-programmer-SCJP/certification/PriorityQueue
https://coderanch.com/t/270583/java-programmer-SCJP/certification/PriorityQueue
https://coderanch.com/forums/posts/list/473699#2121552
 
this is supposed to be a surprise, but it smells like a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic