File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how to prove that Hashtable is not ordered Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how to prove that Hashtable is not ordered" Watch "how to prove that Hashtable is not ordered" New topic
Author

how to prove that Hashtable is not ordered

zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
thanks in advance
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Create a Hashtable, add elements to it, and iterate over them. The output will not be ordered...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

To add to that the output might even be different for different JVMs. It depends on the hashing algorithm used by the hash table implementation in question.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
Ankit Garg wrote:Create a Hashtable, add elements to it, and iterate over them. The output will not be ordered...

thank you.
I think i misunderstood the phrase "not ordered".
I thought it means the output will be in different order every time I iterate through it
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Generally ordered means that the order of the iteration is predictable i.e. you can tell what the output will be by looking at the code. Like ArrayList maintains insertion order i.e. if you iterate over it, you'll get the elements in the order in which you inserted them...
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

order : insertion order or sort order. Hashtable is none hence unordered.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
Ankit Garg wrote:order : insertion order or sort order. Hashtable is none hence unordered.

so it means the elements in Hashtable are ordered according to some algorithm used by the Hashtable implementation, though it's neither insertion order nor sort order.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

zheng li wrote:so it means the elements in Hashtable are ordered according to some algorithm used by the Hashtable implementation, though it's neither insertion order nor sort order.


No, that isn't right, at least not quite right. The algorithm in Hashtable is used to hash its keys. It's true that as a side effect of this algorithm, the elements appear in some order. But saying that they are ordered according to that algorithm is the same as throwing a handful of rocks downstairs. The rocks land on different steps, so they have an ordering. You could say the rocks were ordered by gravity and friction, but that wouldn't be very meaningful.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

zheng li wrote:
Ankit Garg wrote:order : insertion order or sort order. Hashtable is none hence unordered.

so it means the elements in Hashtable are ordered according to some algorithm used by the Hashtable implementation, though it's neither insertion order nor sort order.

To some extent you are right, Hashtable indeed has an internal ordering of elements otherwise you might see repeat elements while iterating over it and some elements might not appear at all. But as Paul said, the ordering is not kinda predictable, so its better to say that Hashtable is not ordered.

PS: I didn't write what's written in your quote, Nitish said that
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to prove that Hashtable is not ordered