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

U

u126561

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.



Jun
 
B

Bjorn Abelli

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.

Here's one way:

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

Collections.sort(list);

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 http://www.x-privat.org/join.php
 
U

u126561

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

sks

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.

TreeMap ?
 
M

Matt Humphrey

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.

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.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
U

u126561

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.

Cheers~~
 
M

Mark Thomas

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

Mark
 
B

Bjorn Abelli

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.

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 http://www.x-privat.org/join.php
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top