2

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

asked Mar 29, 2011 at 15:58
1
  • 1
    I think May be this isn't possible Commented Mar 29, 2011 at 16:04

4 Answers 4

2

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)

answered Mar 29, 2011 at 16:24
1
  • Can you provide me some link that gives me basic knowledge of collections as I am not familiar with it Commented Mar 29, 2011 at 16:41
0

You probably want to do something like

Class key = Class.forName(KeyType);
Class value = Class.forName(ValueType);
return new HashTable<key, value>();
answered Mar 29, 2011 at 16:05
2
  • @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.... Commented Mar 29, 2011 at 16:13
  • Class key=> Its a class how come it will be treated as a type Commented Mar 29, 2011 at 16:29
0
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));
}
answered Mar 29, 2011 at 16:17
0

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.

answered Mar 29, 2011 at 16:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.