• 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

TreeXxx VS PriorityQueue

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I understand because the TreeSet return an sorted result. But I dont understand the result given by an iteraction with PriorityQueue. Can anyone help me with this.. please take the example:



This code give the following outputs:
,a,aaa,b,c,d,e,f,g,i,z,zzz
,a,aaa,d,c,b,f,e,i,g,zzz,z

I understood the first line of output, because its from a sorted set (TreeSet). But how about the second output (from PriorityQueue)? Can anyone help me?

Thank you all.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Java API wrote:
The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).



This would solve your problem .
 
Douglas Boff Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah! Its clear for me now!

Thank you Vijitha Kumara
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you understand what's going on? Because the answer, as it relates to the question, is a little confusing.

PriorityQueue is a sorted collection. If you instantiate a PriorityQueue without using a Comparator, then the sort order is established by the compareTo() method (which all elements inserted to the PriorityQueue must implement.) You can also use a Comparator by using the appropriate PriorityQueue constructor. In the case of String, which is the case in your example, String implements Comparable<String>, which will cause lexicographical ordering.

But: PriorityQueue inherits iterator() from Collection, and that method returns an Iterator which doesn't guarantee any specific ordering. Therefore, your second line of output is a fluke (you shouldn't rely on that order.) However, if you polled the queue sequentially, you would get the elements in the lexicographical sorted order in a reliable way.
 
Douglas Boff Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ruben Soto

I understood the PriorityQueue is a sorted collection. And TreeSet are sorted collection too.

But the difference is in the implementation of iterator method,
used implicitly in for-each by the compiler.





I'm sure? Thank you Ruben Soto
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Douglas Boff Nandi wrote:
I'm sure? Thank you Ruben Soto


It sounds like I offended you, although I was trying to help you. The response talks about a behavior which was not exposed by the output that you offered.

EDIT: Nevermind, it seems I didn't pay attention to your second line of output.

Sorry for the confusion, Douglas Boff Nandi.
 
Douglas Boff Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I m sorry.. its a problem caused because I am not so good with English language! Im from Brazil and I speak portuguese here.

I only asked to you if my reasoning is correctly.

Don't worry, Of course you helped me!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Douglas Boff Nandi wrote:I m sorry.. its a problem caused because I am not so good with English language! Im from Brazil and I speak portuguese here.

I only asked to you if my reasoning is correctly.

Don't worry, Of course you helped me!


Well, Vijitha did all the helping. I misread your post (my fault.)

Your English is just fine, by the way.

Boas tardes,

Ruben
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic