• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iterator

 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • 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 ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic