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

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide me some link that gives me basic knowledge of collections as I am not familiar with it
0

You probably want to do something like

Class key = Class.forName(KeyType);
Class value = Class.forName(ValueType);
return new HashTable<key, value>();

2 Comments

@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....
Class key=> Its a class how come it will be treated as a type
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));
}

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.