I have a method that creates a hashtable..that method's signature is
void CreateHashTable(String KeyType,String ValueType)
I want something like this
Hashtable<KeyType,ValueType> hashtable1;
so that I create my hashtable key,value according to my requirement But How ?? I need datatype instead of a String I think it is understandable
Any help should be greatly appreciated Thanks
-
1I think May be this isn't possibleahmed saud– ahmed saud03/29/2011 16:04:58Commented Mar 29, 2011 at 16:04
4 Answers 4
Generics are a compile time feature so anything you do at runtime e.g. look up a class by name, is beyond the ability of the compiler to determine. You have to provide a Class<K> which the compiler can determine something about.
BTW: Don't use Hashtable unless you have to. It was replaced by J2SE Collections in Java 1.2 (1998)
-
Can you provide me some link that gives me basic knowledge of collections as I am not familiar with itahmed saud– ahmed saud03/29/2011 16:41:40Commented Mar 29, 2011 at 16:41
You probably want to do something like
Class key = Class.forName(KeyType);
Class value = Class.forName(ValueType);
return new HashTable<key, value>();
-
@Jan Zyka, ha, I wasn't paying attention to that. @Simon Nickerson, I didn't have a chance to test it and I wasn't sure if it would work. I imagine there's no way to do this then given two string inputs....Chris Thompson– Chris Thompson03/29/2011 16:13:20Commented Mar 29, 2011 at 16:13
-
Class key=> Its a class how come it will be treated as a typeahmed saud– ahmed saud03/29/2011 16:29:33Commented Mar 29, 2011 at 16:29
public static <K,V> HashMap<K,V> create(Class<K> keytype, Class<V> valtype) {
return new HashMap<K,V>();
}
Given only the strings specifying the object type, you could Class.forName but this won't give you a typesafe generic, i.e.:
public static HashMap create(String keyClassName, String valClassName) {
return create(Class.forName(keyClassName), Class.forName(valClassName));
}
What are you going to do with a Hashtable (or Hashmap if you don't need thread safety) or other collection when you don't know what types it takes? At some point you'll need to know what's in your collection to work with the values, unless you're truly looking for type safety with type indifference.
If you know what you want out, e.g. a Hashtable<String, String>
you can use the "diamond operator" of Java 7. Effective Java by Josh Bloch shows how to do the same thing in Java 5. A PDF of the generics chapter is here, search for the word "factory" to be taken to the example.
Both those require you provide a template of what you want ahead of time, e.g. HashMap<String, String>
and not just the words "String" and "String". I think this article on factory chains describes what you're looking for, but it's not easy.
The best advice I can give you is to rethink your design. I'm quite certain you don't really need to do what you're trying to do. Maybe if you explained the problem you're trying to solve we can come up with a better approach.