我有一个场景,其中有一个带有Future对象的bean(下面的A类)。我有另一个类(下面的类B),它是一个单例,具有HashMap作为类型的实例变量,并实现了一个接口(下面的TestInterface),该接口又实现了Callable。
根据当前方案,用户只能传递Object类型的Future,但是根据我的新要求,我希望使用Generic Type的Future Object。我已经修改了似乎有效的代码,但是有很多警告,而且我不确定我的更改是否正确。我确定在某些情况下代码会失败。我面临的主要问题是在单例类中初始化GenericType的HashMap。有人可以帮我吗?
下面的代码是现有代码的示例。
接口测试界面:
interface TestInterface extends Callable<Void>{
void doSomething(Future<Object> future, String id);
}
A级
class A{
private Future<Object> future;
private CustomInteraface a;
public A(Future<Object> future, CustomInteraface a){
//do init
}
//getters and setters
}
B级
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<Object> future, String id){
if(map.contains(id)){
//Do something
}
else{
A a = new A(future, null);
map.put();
}
}
}
我为泛型者所做的更改
接口测试界面:
interface TestInterface extends Callable<Void>{
<T> void doSomething(Future<T> future, String id);
}
A级
class A<T>{
private Future<T> future;
private CustomInteraface a;
public A(Future<T> future, CustomInteraface a){
//do init
}
//getters and setters
}
B级
Class B implements TestInterface{
private HashMap<String, A> map = new HashMap();
private static B monitor = new B();
public Void call(){
HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
A a = (A) pair.getValue();
//Code will definitely fail here as I'm trying to cast a future object of generic type to Object class
Future<Object> future = a.getFuture();
// Do something
}
public void doSomething(Future<T> future, String id){
if(map.contains(id)){
//Do something
}
else{
A<T> a = new A<T>(future, null);
map.put();
}
}
}
答案 0 :(得分:0)
如果您可能将A<T>
个异构类型放入映射中,则需要使用通配符声明该映射:
private HashMap<String, A<?>> map = new HashMap();
您将通过以下方式从地图中获取一个值:
// The cast was only necessary because A by itself is a raw type.
HashMap.Entry<String, A<?>> pair = it.next();
A<?> a = pair.getValue();
Future<?> future = a.getFuture();
// Note that future.get() yields an Object
并将其放入地图中,例如:
public void doSomething(Future<?> future, String id){
...
A<?> a = new A<>(future, null);
map.put(id, future);
...
}
如果您需要T
中将来的doSomething
返回类型,则可以在方法上声明类型变量:
public <T> void doSomething(Future<T> future, String id){