【发布时间】:2018-07-29 18:46:04
【问题描述】:
我需要创建通用对象的 Java 缓存。我正在尝试执行以下操作,但这不会编译。
public class Example<T> {
public static class ExampleCache {
private Map<String, Example<?>> cache = new ConcurrentHashMap<>();
public <T> Example<T> getExample(String name) {
return cache.computeIfAbsent(name, k -> new Example<T>());
}
}
}
带有cache.computeIfAbsent 的行会产生错误
"Error:(16, 35) java: incompatible types: Example<capture#1 of ?> cannot be converted to Example<T>
有什么建议吗?
【问题讨论】: