Accessing the data type of a Generic structure

Good afternoon, Ladies and Gentlemen!

So, I've got this method loadStuff, that I want to be able to take as input a Hashtable that maps Strings to other things. Right now, my method definition looks like:staticint loadStuff(File infile, Hashtable<String, ?> Storage)

but I get a compile time error

"cannot find symbol

symbol : method put(java.lang.String,StatsRecord)

location: class java.util.Hashtable<java.lang.String,capture of ?>

Storage.put(PreHash.getname(), PreHash);"

Is there a way around this?

Also: I'd like some way to get at the type of hashtable that I'm passing to the function. So what I'm looking for would be a method that, if I passed it Awesome =new List<AwesomeClass>()

would return AwesomeClass.

Any thoughts?

[926 byte] By [MisterMoofusa] at [2007-10-3 3:23:59]
# 1

Concerning compilation error:

You are trying to put (write) data to your Storage hashtable.

But

Storage.put("I磎 a string", new StatsRecord());

is not allowed with wildcarded types. Read Angelika Langer's FAQ for this.

Way around: do not use unbounded wildcard. Use bounded wildcards instead (this won磘 be sufficient in your case). Use concrete parametrized types, that is Hashtable<String, Integer> f.e.

Concerning AwesomeClass:

as far as I know, there is no way to obtain generics type information during runtime, since nearly all of that information is removed due to "type erasure"

The only thing you could do is a parameterized method (doesn't have to be static)

Class cool {

....

void <U> methodName(Class<U> clazz, List<U> list) {

// use reflection here f.e.

// clazz.isInstance(..)

}

}

A call to this method could be:

Cool veryCoolInstance = new Cool();

veryCoolInstance.<Integer>methodName(Integer.class, yourList);

Best regards

DTN

DoodleTheNoodlea at 2007-7-14 21:16:39 > top of Java-index,Core,Core APIs...
# 2
Ah, okay. That makes sense. Thank you very much for explaining that!
MisterMoofusa at 2007-7-14 21:16:39 > top of Java-index,Core,Core APIs...