• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterator

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1 2 4 3 5
a c b d e

Why is iterator iterating numbers always in natural order but never strings ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:
1 2 4 3 5
a c b d e

Why is iterator iterating numbers always in natural order but never strings ?


The Java doc for PriorityQueue says

The iterator does not return the elements in any particular order.


So it may iterate according to natural order or it may not. If you look at the source code for PriorityQueue it might give you a clue as to what order objects are returned in, but this could be different in different versions of the JRE. All you need to know is that the order shouldn't be relied on.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if it works with numbers always then why not with strings.It is doing well with TreeSet.


0 1 2 3 4 5
a b c d e f

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:But if it works with numbers always then why not with strings.


As I said, look at the source code. It might just be a coincidence that the integers come out in their natural ordering.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

meeta gaur wrote:Why is iterator iterating numbers always in natural order but never strings ?


Well, from the evidence you showed us, it is.

But if it works with numbers always then why not with strings.It is doing well with TreeSet.


Because TreeSet is defined to return elements in their natural order.

Honestly meeta, I think you could answer an awful lot of these questions for yourself if you took the time to read the documentation before posting.

Winston
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

But if it works with numbers always then why not with strings.It is doing well with TreeSet.


Because TreeSet is defined to return elements in their natural order.

Winston



Yes ,I read documentation and found that PriorityQueue has also natural ordering by default.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a closer look...

meeta gaur wrote:
1 2 4 3 5
a c b d e


That's not the natural order for integers either. If you add the integers in the same relative order as you added the Strings (so 4 2 1 3 5) you'll get the same pattern as the String output.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Have a closer look...

meeta gaur wrote:
1 2 4 3 5
a c b d e


That's not the natural order for integers either. If you add the integers in the same relative order as you added the Strings (so 4 2 1 3 5) you'll get the same pattern as the String output.



I was confused.Thank you
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic