java - Why a hashtable does not maintain order of insertion? -
this question has answer here:
- how hash table work? 14 answers
if hash table array of linked-list elements , hash code index of element in array why hash table not maintain order of insertion ??
to brief, hash table (dictionary) not maintain total order of insertions because doesn't need to. abstract data type supports ammortized o(1) insertions, deletions, , searches, not support enumeration, , not impose order on elements in key set.
hashtable implements dictionary, , total order of insertions not retained because insertions different hash values map different chains. in case of dictionary implementation using chaining, keys colliding hashes stored in linked list (as stated in question), , indeed maintained in order of insertion. there exist many other (faster) implementations of dictionaries not have property. please see thees lecture notes discussion of open addressing (a dictionary implementation pattern not retain insertion order of colliding elements). open addressing double hashing, in particular, not impose order on elements stored in key set.
Comments
Post a Comment