在Java中是否有一种方法可以使用映射,其中值的类型参数与键的类型参数相关联?我想写的内容如下:
public class Foo {
// This declaration won't compile - what should it be?
private static Map<Class<T>, T> defaultValues;
// These two methods are just fine
public static <T> void setDefaultValue(Class<T> clazz, T value) {
defaultValues.put(clazz, value);
}
public static <T> T getDefaultValue(Class<T> clazz) {
return defaultValues.get(clazz);
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,只要值的类型与Class对象的类型匹配,我就可以对Class对象存储任何默认值.我不明白为什么不允许这样做,因为我可以确保在设置/获取类型正确的值时.
编辑:感谢cletus的回答.我实际上并不需要地图本身的类型参数,因为我可以确保获取/设置值的方法的一致性,即使它意味着使用一些稍微丑陋的演员表.
我正在尝试创建一个泛型类型,它保留了为以后使用而创建的自身版本的映射.实际上,它是一个单例模式,每种类型都有一个实例.我到目前为止的代码是:
public class FieldBinder<T> {
static final Map<Class<? extends Object>,FieldBinder<? extends Object>> instanceMap =
new HashMap<Class<? extends Object>,FieldBinder<? extends Object>>();
private FieldBinder() {}
synchronized public static <V extends Object> FieldBinder<V> getInstance(Class<V> klass) {
if(!instanceMap.containsKey(klass)) {
instanceMap.put(klass, new FieldBinder<V>());
}
return (FieldBinder<V>)instanceMap.get(klass);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我仍然不确定我"做得对".感觉我应该能够指定集合是(Class - > FieldBinder).IDE警告返回语句的事实只会强化这种想法.
有没有更好的方法来处理这个?
注意:这个问题看起来非常密切相关,但是远远不够,我无法弄清楚如何将信息应用到我自己的问题中.
我有以下测试代码:
FileSystem fs = FileSystems.getDefault();
Path conf = fs.getPath(".");
WatchKey key = null;
try {
WatchService watcher = fs.newWatchService();
conf.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
while(true) {
key = watcher.take(); // waits
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
if (StandardWatchEventKinds.OVERFLOW == kind) continue;
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Path file = ev.context();
System.out.println(file);
}
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e.getMessage(), e);
}
Run Code Online (Sandbox Code Playgroud)
编译器unchecked cast发出与该行相关的警告
WatchEvent<Path> ev = (WatchEvent<Path>)event;
Run Code Online (Sandbox Code Playgroud)
因为作为一个event出来,编译器无法判断在运行时它是否真的要包含一个,而不是其他东西.key.pollEvents()WatchEvent<?>Path …