This week's book giveaway is in the Agile and Other Processes forum.We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!See this thread for details.
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
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# TreeXxx VS PriorityQueue

Ranch Hand
Posts: 34
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Yeah! Its clear for me now!

Thank you Vijitha Kumara

Ranch Hand
Posts: 1032
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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

 what if we put solar panels on top of the semi truck trailer? That could power this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com