• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to prove that Hashtable is not ordered

 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks in advance
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a Hashtable, add elements to it, and iterate over them. The output will not be ordered...
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
order : insertion order or sort order. Hashtable is none hence unordered.
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic