how to sort the keys from Hashtable into Alphabet order when use for output display

Discussion in 'Java' started by u126561, May 7, 2006.

  1. u126561

    u126561 Guest

    Hi guys,
    I have a Hashtable<String, ArrayList>, the ArrayList is to store the
    path of where the "string" appear. Now, I need to output the string and
    the ArrayList (as a pair). However, they (the keys) are not in the
    order I need (Alphabet order). Does anyone have any idea about how to
    solve it? Thank you.

    u126561, May 7, 2006
    1. Advertisements

  2. u126561

    Bjorn Abelli Guest

    Here's one way:

    ArrayList<String> list =
    new ArrayList<String>(hashtable.keySet());


    for (String key : list)
    ArrayList value = hashtable.get(key);

    // Here you can "output" them in any way you like...

    You're a bit unclear on what you mean by "output the ArrayList", as you
    don't say what the ArrayList contains.

    // Bjorn A

    Inviato da X-Privat.Org - Registrazione gratuita
    Bjorn Abelli, May 7, 2006
    1. Advertisements

  3. u126561

    u126561 Guest

    thanks, mate
    the ArrayList contains file path (abolutepath)

    I can see you are sorting the ArrayList now, but I need to sort the key
    not the value.
    u126561, May 7, 2006
  4. u126561

    sks Guest

    TreeMap ?
    sks, May 7, 2006
  5. Bjorn's code puts the keys into a separate ArrayList and sorts that. The
    for-loop then gets each value ArrayList from your original hashtable one at
    a time, in the order given by the sorted keys.

    Matt Humphrey
    Matt Humphrey, May 7, 2006
  6. u126561

    Bjorn Abelli Guest

    You're welcome... ;-)
    In what *form* do you store this "absolutepath".

    The most common form I come across when representing a file path, is to
    simply use a String.

    Why bury it into an ArrayList at all?

    // Bjorn A

    Inviato da X-Privat.Org - Registrazione gratuita
    Bjorn Abelli, May 8, 2006
  7. u126561

    u126561 Guest

    this is because the same "word" may appear in more than 1 document, so
    that there will be more than 1 path need to stored, therefore ArrayList
    or String[] needed.

    u126561, May 8, 2006
  8. u126561

    Mark Thomas Guest

    No, he's made an ArrayList of the keys & sorted that. Having got the
    keys in the correct order, you can extract the values from the original

    Mark Thomas, May 8, 2006
  9. u126561

    Bjorn Abelli Guest

    Alright, that makes more sense.

    One more thought though.

    If the "sorting" occurs frequently, and there are many keys, maybe you
    should look into a slightly different strategy, e.g. back the hashtable
    consistently with a sorted collection containing the keys at all time, or
    use a completely different collection from start.

    // Bjorn A

    Inviato da X-Privat.Org - Registrazione gratuita
    Bjorn Abelli, May 8, 2006
    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.