For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2006 > Get the key of an element in Hashtable/HashMap









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Get the key of an element in Hashtable/HashMap
Allan Bruce

2006-01-23, 7:06 pm

I am loading a preferences file which has many lines of the following
format:

PREFERENCE: VALUE

I am loading the file line by line and using String#split() to get the
preference and the value. I am then adding to a Hashtable. When it comes
to saving I would like to iterate through the elements set in the Hashtable
and save them however I need access to the key I stored them in, but I cant
see how to do this. Do I need to use a different data structure? Something
like this is what I would like:

Enumeration e = mPrefs.elements();
while (e.hasMoreElements)
{
String lValue = (String)e.nextElement();
String lPref = (String)e.getKey();
savePref(lPref, lValue);
}

Any ideas?
Thanks
Allan


Roedy Green

2006-01-23, 7:06 pm

On Mon, 23 Jan 2006 22:47:30 -0000, "Allan Bruce" <amb@abc.net> wrote,
quoted or indirectly quoted someone who said :

>I am loading the file line by line and using String#split() to get the
>preference and the value.


if you used = instead of : you could use Properties.load
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Allan Bruce

2006-01-23, 7:06 pm


"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid> wrote in
message news:i0oat1h9rd8kvqjnq1hhbo93fa0efne5d8@
4ax.com...
> On Mon, 23 Jan 2006 22:47:30 -0000, "Allan Bruce" <amb@abc.net> wrote,
> quoted or indirectly quoted someone who said :
>
>
> if you used = instead of : you could use Properties.load


I cant unfortunately - the prefs file is defined by another application.
Thanks
Allan


Thomas Fritsch

2006-01-23, 7:06 pm

"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid> wrote:
> On Mon, 23 Jan 2006 22:47:30 -0000, "Allan Bruce" <amb@abc.net> wrote,
> quoted or indirectly quoted someone who said :
>
>
> if you used = instead of : you could use Properties.load


You can use Properties.load, even when the delimiter is ":" or ": " (like in
the original poster's example).

Quoted from the API doc of java.util.Properties#load,
<http://java.sun.com/j2se/1.4.2/docs...rties.html#load(java.io.InputStream)>
<quote>
[...] The key contains all of the characters in the line starting with the
first non-white space character and up to, but not including, the first
unescaped '=', ':', or white space character other than a line terminator.
[...] Any white space after the key is skipped; [...]
</quote>

--
"TFritsch$t-online:de".replace(':','.').replace('$','@')


Roedy Green

2006-01-23, 9:57 pm

On Mon, 23 Jan 2006 22:47:30 -0000, "Allan Bruce" <amb@abc.net> wrote,
quoted or indirectly quoted someone who said :

> I am then adding to a Hashtable.


Use a HashMap instead with the slick JDK 1.5 for:each enumerations.

You can enumerate keys, values or entries (key,value pairs).

See sample code at http://mindprod.com/jgloss/hashmap.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Lothar Kimmeringer

2006-01-24, 3:58 am

Allan Bruce wrote:

> Enumeration e = mPrefs.elements();


e = mPrefs.keys();

> while (e.hasMoreElements)
> {


String key = (String) e.nextElement();
savePref(key, (String) mPrefs.get(key));

> }


> Any ideas?


Yes, see above ;-)


Best regards, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
Allan Bruce

2006-01-24, 7:05 pm


"Roedy Green" <my_email_is_posted_on_my_website@munged.invalid> wrote in
message news:4d2bt1p93h5oqeoum1g4iniaa5o54l5s4f@
4ax.com...
> On Mon, 23 Jan 2006 22:47:30 -0000, "Allan Bruce" <amb@abc.net> wrote,
> quoted or indirectly quoted someone who said :
>
>
> Use a HashMap instead with the slick JDK 1.5 for:each enumerations.
>
> You can enumerate keys, values or entries (key,value pairs).
>
> See sample code at http://mindprod.com/jgloss/hashmap.html


I cant use JDK1.5 since my application has to work on Mac OS 10.2.8 unles
anybody knows how to install Java1.5 on this OS?
Allan


Allan Bruce

2006-01-24, 7:05 pm


"Lothar Kimmeringer" <news200601@kimmeringer.de> wrote in message
news:ef9tjab0y7cr.dlg@kimmeringer.de...
> Allan Bruce wrote:
>
>
> e = mPrefs.keys();
>
>
> String key = (String) e.nextElement();
> savePref(key, (String) mPrefs.get(key));
>
>
>
> Yes, see above ;-)
>
>
> Best regards, Lothar


That looks like the solution, but when accessing e.nextElement() the program
just hangs. I checked the contents of the Hashtable and everything seems to
be in order. Not sure why it is doing this...
Allan


Roedy Green

2006-01-24, 7:06 pm

On Tue, 24 Jan 2006 15:25:26 -0000, "Allan Bruce" <amb@abc.net> wrote,
quoted or indirectly quoted someone who said :

>I cant use JDK1.5 since my application has to work on Mac OS 10.2.8 unles
>anybody knows how to install Java1.5 on this OS?
>Allan


I have sample code using HashMap without generics or for:each. See
http://mindprod.com/jgloss/hashmap.html under "manual boxing"
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com