multiple entries for a key in Preferences

R

Rajarshi Guha

Hi, I have an app in which I'd like to keep track of multiple user
specified directories. In the past I have used a dot file in the users
home directory and simply parsed that.

However I'd like to use the Preferences API but I'm not sure how I can
handle this situation.

So for example I need to keep track of directories DIRA, DIRB and DIRC.

All of them would correspond to a dirEntries

So I could do something like:

Preferences prefs =
Preferences.userRoot().node("/net/guha/apps/cdkdesc/dirEntries") ;

However if I now go to do

prefs.put("Entry", "DIRA");

I can't put in the others using the same key.

One possibility I thought of using the hash() method for a given String,
but I'm not sure that will always be unique.

The only other approach I can think of is to make the key and the value
the same.

Are there any better approaches to this problem?

Thanks,
Rajarshi
 
R

Rhino

Rajarshi Guha said:
Hi, I have an app in which I'd like to keep track of multiple user
specified directories. In the past I have used a dot file in the users
home directory and simply parsed that.

However I'd like to use the Preferences API but I'm not sure how I can
handle this situation.

So for example I need to keep track of directories DIRA, DIRB and DIRC.

All of them would correspond to a dirEntries

So I could do something like:

Preferences prefs =
Preferences.userRoot().node("/net/guha/apps/cdkdesc/dirEntries") ;

However if I now go to do

prefs.put("Entry", "DIRA");

I can't put in the others using the same key.

One possibility I thought of using the hash() method for a given String,
but I'm not sure that will always be unique.

The only other approach I can think of is to make the key and the value
the same.

Are there any better approaches to this problem?

Thanks,
Rajarshi

I had a comparable situation where I wanted to store an unknown number of
Strings of variable length in a single preference key. I simply concatenated
all of the Strings together into a single String, separated by a delimiter
of my choosing, then stuffed the concatenation String into a preference key.
When I retrieved the key again, I had to parse the values but that was easy
since I knew what delimiter I had placed after each of the Strings within
the concatenation.

I'm not saying that's the BEST way to handle the problem - someone else may
suggest something better - but it certainly worked well for me.

Rhino
 

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

Forum statistics

Threads
473,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top