aspose file tools*
The moose likes Beginning Java and the fly likes Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iterator" Watch "Iterator" New topic
Author

Iterator

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305


1 2 4 3 5
a c b d e

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


My one regret in life is that I am not someone else.
- Woody Allen
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3732
    
  16
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.


Joanne
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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

Joined: Aug 05, 2005
Posts: 3732
    
  16
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

Joined: Mar 17, 2011
Posts: 8159
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

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

Joined: Apr 06, 2010
Posts: 4457
    
    8

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

Joined: Dec 05, 2012
Posts: 305

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Iterator