usage of collections.. please suggest

hello everyone,

I need to setup a simple way of retrieving data using the collections frameworks here is the problem

eg,

codevalues

100 "a","b", "c"

500 "w","er", "oo"

..

..etc (limited no. of codes and values)

previously I was using the if-else statements, ie,

if value == "a" ||value =="b"

code = 100;

....etc

now I was just studying the collections framework and thought that I can retrieve the code from the values much easier using them.

Any ideas on how I can use collections?

currently I was thinking of using a Hashtable, eg,

-

String [ ] value = {"a", "b","w"...etc};

int [ ] code = {100,100,500,..etc};

table = new Hashtable();

for(i=0;i<xx;i++)

table.put(value,code);

-

and then for retrieval use

getCode = table.get(value);

--

is there a better option than this, maybe a way to use more than one collection

thanks>

[1013 byte] By [KsRa] at [2007-11-27 2:15:34]
# 1
Yes...I think you are in the right direction there. You'd want to use a hashed collection. Hashtable or HashMap would do .Budyanto
Budyanto.Himawana at 2007-7-12 2:12:31 > top of Java-index,Java Essentials,New To Java...
# 2

thanks for the quick reply,

actually I was wondering if it is possible to get the key from and group of values. let me explain the problem a bit..

currently severaly of my keys(the letter strings) are pointing a the same value(the codes)

codevalues

100"a", "b", "c"

is there a way in which my code can be the 'key' and "a","b", "c" the values...ie, i can get the corresponding key when I search a string of values. Currently I am doing it the other way round, so I'm repeating many values in the code array.

thanks

KsRa at 2007-7-12 2:12:31 > top of Java-index,Java Essentials,New To Java...
# 3

Hi,

So if I understand you correctly, the final goal is you want to get a code, given a value.

The most efficient way to do this is to use a hashed collection and make the "value" the key so you can get a constant time ((O(1)) search time for the code.

So as you pointed out, you'd have multiple values that map to the same code:

Key| Value

"a"100

"b"100

If you'd like you can have the Key or Value be a List collection type. For example:

Key| Value

"a", "b"100

But now the search becomes more indirect and less efficient.

Budyanto

Budyanto.Himawana at 2007-7-12 2:12:31 > top of Java-index,Java Essentials,New To Java...
# 4
thanks a lot, trade offs between efficiency and ease are always there i guess..thank for you suggestionscheers...
KsRa at 2007-7-12 2:12:31 > top of Java-index,Java Essentials,New To Java...