Ordering of hashtable keys

K

Kevin McMurtrie

"Boris Punk said:
Hi

Could someone tell me what determines the order of the keys in a hashtable?

If the JavaDoc doesn't state an order then it's a mix of JVM version,
initial table size, insertion order, and _lots_ of bit hashing. After
all, a hashing algorithm works best when it eliminates bucket mapping
patterns.

Use something like LinkedHashMap or TreeMap if order matters.
 
L

Lew

Kevin said:
If the JavaDoc doesn't state an order then it's a mix of JVM version,
initial table size, insertion order, and _lots_ of bit hashing. After
all, a hashing algorithm works best when it eliminates bucket mapping
patterns.

Use something like LinkedHashMap or TreeMap if order matters.

And don't use 'java.util.Hashtable' even when order doesn't matter. It's been
out of date since '98.
 
M

markspace

Boris said:
Hi

Could someone tell me what determines the order of the keys in a hashtable?


Hash tables work by distributing their keys randomly, and therefore
presumably evenly, in an array. The hash code and the hashing algorithm
that uses the hash code are basically random number generators that
produce indexes into this array.

So the order of the keys is random, and as Patricia pointed out will
change at runtime if the array needs to be re-hashed.
 
B

Boris Punk

Something I've noticed without digging into the code-that key strings that
are almost the same apart from 1 character are next to each other-figure
that one out!
 
L

Lew

Boris said:
Something I've noticed without digging into the code-that key strings that
are almost the same apart from 1 character are next to each other-figure
that one out!

I don't fully understand that. What do you mean by "key strings" and "next to
each other"?

Does it matter where the one-character difference is in the strings?

Can't figure out a problem so incompletely specified.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top