Ordering of hashtable keys

Discussion in 'Java' started by Boris Punk, Jul 17, 2010.

  1. Boris Punk

    Boris Punk Guest


    Could someone tell me what determines the order of the keys in a hashtable?
    Boris Punk, Jul 17, 2010
    1. Advertisements

  2. 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

    Use something like LinkedHashMap or TreeMap if order matters.
    Kevin McMurtrie, Jul 17, 2010
    1. Advertisements

  3. Boris Punk

    Lew Guest

    And don't use 'java.util.Hashtable' even when order doesn't matter. It's been
    out of date since '98.
    Lew, Jul 17, 2010
  4. Boris Punk

    markspace Guest

    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.
    markspace, Jul 17, 2010
  5. Boris Punk

    Boris Punk Guest

    Boris Punk, Jul 18, 2010
  6. Boris Punk

    Boris Punk Guest

    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!
    Boris Punk, Jul 19, 2010
  7. Boris Punk

    Lew Guest

    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.
    Lew, Jul 19, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.