避免Java泛型警告?
我不熟悉Java的泛型特性,在我的一个方法中遇到了一些困难。Eclipse给了我几个警告,我希望避免这些警告。该方法将布尔标志作为其参数,并根据布尔值返回避免Java泛型警告?,java,generics,warnings,Java,Generics,Warnings,我不熟悉Java的泛型特性,在我的一个方法中遇到了一些困难。Eclipse给了我几个警告,我希望避免这些警告。该方法将布尔标志作为其参数,并根据布尔值返回List或List。我可以将方法分成两部分,每种情况一个,但我宁愿不这样做,以便将逻辑保持在一个地方 以警告作为注释的简化方法: private <T> List<T> getList(boolean returnInt) { // List is a raw type. References to generi
List
或List
。我可以将方法分成两部分,每种情况一个,但我宁愿不这样做,以便将逻辑保持在一个地方
以警告作为注释的简化方法:
private <T> List<T> getList(boolean returnInt) {
// List is a raw type. References to generic type List<E> should be parameterized
List returnList;
if (returnInt) {
returnList = new ArrayList<Integer>();
} else {
returnList = new ArrayList<String>();
}
if (mShowNew) {
if (returnInt) {
// Type safety: The method add(Object) belongs to the raw type List.
// Refs to generic type List<E> should be parameterized.
returnList.add(ID_NEW);
} else {
// Type safety: The method add(Object) belongs to the raw type List.
// Refs to generic type List<E> should be parameterized.
returnList.add("New");
}
}
if (mResume) {
if (returnInt) {
returnList.add(ID_RESUME);
} else {
returnList.add("Resume");
}
}
// Pattern continues
// Type safety: The expression of type List needs unchecked conversion to
// conform to List<T>
return resultList;
}
private List getList(boolean returnInt){
//列表是原始类型。对泛型类型列表的引用应参数化
列表返回列表;
if(returnInt){
returnList=newarraylist();
}否则{
returnList=newarraylist();
}
如果(mShowNew){
if(returnInt){
//类型安全:方法add(Object)属于原始类型列表。
//对泛型类型列表的引用应参数化。
返回列表。添加(ID\u新建);
}否则{
//类型安全:方法add(Object)属于原始类型列表。
//对泛型类型列表的引用应参数化。
返回列表。添加(“新”);
}
}
如果(mResume){
if(returnInt){
返回列表。添加(ID\U简历);
}否则{
返回名单。添加(“简历”);
}
}
//模式继续
//类型安全性:类型列表的表达式需要未选中转换为
//符合清单
返回结果列表;
}
我可以更改什么以避免这些警告?如果有更好的方法,请朝正确的方向推动。创建一个新类,该类同时包含
整数和字符串
例如
按要求填写此表格,并将其用作
List<Result> returnList;
列表返回列表;
当然,让您的方法返回
private List<Result> getList(boolean returnInt) {
private List getList(boolean returnInt){
如果该参数仅用于确定重新返回列表的嵌套类型,则可以将该类型作为参数来指定所需的列表类型
这可能是这样的:
private <T> List<T> getList(Class<T> listType) {
if (!(listType.getClass().equals(Integer.class)
|| listType.getClass().equals(String.class))) {
throw new IllegalArgumentException(
String.format("Type '%s' is currently not supported.", listType.getClass().getName()));
}
// v--- the missing type specification was causing the warnings you mentioned
List<T> returnList = new ArrayList<>();
if (mShowNew) {
if (listType.getClass().equals(Integer.class)) {
returnList.add(ID_NEW);
} else {
returnList.add("New");
}
}
if (mResume) {
if (listType.getClass().equals(Integer.class)) {
returnList.add(ID_RESUME);
} else {
returnList.add("Resume");
}
}
//...
return resultList;
}
私有列表getList(类listType){
如果(!(listType.getClass().equals)(Integer.class)
||listType.getClass().equals(String.class))){
抛出新的IllegalArgumentException(
String.format(“类型“%s”当前不受支持。”,listType.getClass().getName());
}
//v---缺少的类型规范导致了您提到的警告
List returnList=new ArrayList();
如果(mShowNew){
if(listType.getClass().equals(Integer.class)){
返回列表。添加(ID\u新建);
}否则{
返回列表。添加(“新”);
}
}
如果(mResume){
if(listType.getClass().equals(Integer.class)){
返回列表。添加(ID\U简历);
}否则{
返回名单。添加(“简历”);
}
}
//...
返回结果列表;
}
为什么不创建两个单独的函数,一个返回int,另一个返回字符串?这样返回两种列表是一个非常糟糕的主意。无论如何,您的原始类型可能是List returnList;
(但同样,这是一个坏主意)。正如@ElliottFrisch所说的Java不是一种动态语言,你不应该弄乱类型系统和泛型(不管你认为它们有多糟糕:-)@ElliottFrisch好的,谢谢你的指针。我会尝试重新编写它,这样就没有必要了。
private <T> List<T> getList(Class<T> listType) {
if (!(listType.getClass().equals(Integer.class)
|| listType.getClass().equals(String.class))) {
throw new IllegalArgumentException(
String.format("Type '%s' is currently not supported.", listType.getClass().getName()));
}
// v--- the missing type specification was causing the warnings you mentioned
List<T> returnList = new ArrayList<>();
if (mShowNew) {
if (listType.getClass().equals(Integer.class)) {
returnList.add(ID_NEW);
} else {
returnList.add("New");
}
}
if (mResume) {
if (listType.getClass().equals(Integer.class)) {
returnList.add(ID_RESUME);
} else {
returnList.add("Resume");
}
}
//...
return resultList;
}