lookup table
Hello there,
I am trying to create a lookuptable. When I get values from database, I have to lookup for that key value pair and then do my operation. I cannot use a properties file here. The only thing I can do is I can create another class for looking up key values. I tried creating a class and used Hashtable to set key values but I could not make it work. Any ideas regarding this.
Thanks
Potti
> I tried creating a class and used Hashtable to set key values but I could not make it work
Then you are doing something wrong. A Hashtable or HashMap is used to store Key/Value pairs so you're on the right track.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
> If you need further help then you need to create a
> [url
> http://homepage1.nifty.com/algafield/sscce.html]Short,
> Self Contained, Compilable and Executable, Example
> Program[/url] that demonstrates the incorrect
> behaviour,
I bet that on doing this he will no longer have a problem!
I am sorry if the formatting is wrong. Its the first time I am doin this. So what I was trying is:
import java.util.Hashtable;
public class Lookup {
public Lookup()
{
Hashtable id = new Hashtable();
id.put("a","bcd");
}
}
now if I want to use in my class where I need to lookup, I instantiate the lookup class, but since it does not have any getters how can I get its value.
Thanks
Potti
Why to you need a Lookup class? Just create and use the Hashtable directly in your program. The Hashtable has the getter methods.
I have lot of key value pairs to lookup. So I tried creating a class for lookup.
I tried passing hashtable like
import java.util.Hashtable;
public class Lookup {
public Lookup(Hashtable h)
{
Hashtable id = new Hashtable();
id.put("a","Federal 945");
}
}
and in the class where I need them I am trying
Hashtable l = new Hashtable();
Lookup el = new Lookup(l);
But when I say l.get(something i.e the key)
, I cannot populate the value of the key in the class where I want the lookup.
May Iam implementing the wrong way. Can you tell me wbout this
Thanks
Potti
No need to send any Hashtable to the lookup() class. Actually no need to have a separate class for Hashtable, but if you want one try below:
public class Lookup {
Hashtable id = new Hashtable();
public putdataLookup(String key, String value)
{
id.put(key,value);
}
public getdataLookup(String key)
{
id.get(key);
}
}
//From the calling class:
Lookup el = new Lookup();
el.putdataLookup("a","Federal 945");
String temp = el.getdataLookup("a");
//temp will be "Fedral 945"
Looks good to me. But the calling class has lot operations to perform. So I was wondering whether I could set all key value 's in other class separately, instead of messing up the calling class. Thanks