我如何获得Vector Arraylist 作为call()的返回值?
我怎么下面的Java代码的工作:我如何获得Vector <type> Arraylist <type>作为call()的返回值?
public class Worker implements Callable<myObject>{
@Override
public ArrayList<myObject> call() throws Exception {
ArrayList<myObject> lst_MyObjects= new ArrayList<myObject>();
return lst_MyObjects;
}
}
错误:返回类型是Callable.call()不兼容
我只是想返回特定类型的容器(Vector<>
/ArrayList<>
等)
只要使用此:
public class Worker implements Callable<List<myObject>>{
通过使用List
,您可以返回ArrayList
或Vector
,但建议您使用ArrayList
而不是Vector
。
哦..多么愚蠢的错误.. 非常感谢您的快速响应 – JustCoding 2013-05-06 09:47:48
它必须是Callable<List<myObject>>
,因为ArrayList<myObject>
是返回类型,而不是myObject。
如果您使用' Callable
好点,这个约束没有得到这个约束。 – 2013-05-06 09:51:42
只要改变方法call()
从ArrayList<myObject>
来Object
java.util.concurrent.Callable返回类型? – 2013-05-06 09:44:22
这里有一个使用Callable的教程:https://blogs.oracle.com/CoreJavaTechTips/entry/get_netbeans_6 – 2013-05-06 09:45:53