对 java.util.Collection.contains 的可疑调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4131303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Suspicious call to java.util.Collection.contains
提问by Cheok Yan Cheng
I am getting the following warning from my NetBeans IDE.
我从 NetBeans IDE 收到以下警告。
Suspicious call to java.util.Collection.contains
Expected type T, actual type Object
May I know what does that means?
我可以知道这是什么意思吗?
This doesn't make sense to me. Both List
and Collection
class's contains
method, are using Object as their method parameter.
这对我来说没有意义。无论List
和Collection
类的contains
方法,使用对象作为自己的方法的参数。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
*
* @author yan-cheng.cheok
*/
public abstract class AbstractCollection<T> implements Collection<T> {
protected List<T> list = new ArrayList<T>();
public boolean contains(Object o) {
// Suspicious call to java.util.Collection.contains
// Expected type T, actual type Object
return list.contains(o);
}
Code snippet from Collection class
来自 Collection 类的代码片段
/**
* Returns <tt>true</tt> if this collection contains the specified element.
* More formally, returns <tt>true</tt> if and only if this collection
* contains at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this collection is to be tested
* @return <tt>true</tt> if this collection contains the specified
* element
* @throws ClassCastException if the type of the specified element
* is incompatible with this collection (optional)
* @throws NullPointerException if the specified element is null and this
* collection does not permit null elements (optional)
*/
boolean contains(Object o);
Code snippet from List class
List 类的代码片段
/**
* Returns <tt>true</tt> if this list contains the specified element.
* More formally, returns <tt>true</tt> if and only if this list contains
* at least one element <tt>e</tt> such that
* <tt>(o==null ? e==null : o.equals(e))</tt>.
*
* @param o element whose presence in this list is to be tested
* @return <tt>true</tt> if this list contains the specified element
* @throws ClassCastException if the type of the specified element
* is incompatible with this list (optional)
* @throws NullPointerException if the specified element is null and this
* list does not permit null elements (optional)
*/
boolean contains(Object o);
回答by dpsthree
In the call to list.contains you are comparing an object to a type T. Casting o to type T should resolve your warning.
在对 list.contains 的调用中,您将对象与类型 T 进行比较。将 o 转换为类型 T 应该可以解决您的警告。
回答by josefx
Calling the contains method with an Object instead of the generic type may be a programming error. Since the code is still valid the compiler will only show a warning.
使用 Object 而不是泛型类型调用 contains 方法可能是编程错误。由于代码仍然有效,编译器只会显示警告。
An example why this warning is necessary:
为什么需要此警告的示例:
List<Long> l = new ArrayList<Long>();
l.add(1l);
l.contains(1);
The code is valid but would always return false. An error that is normally hidden by contains accepting object instead of a generic type, so the compiler is limited to warnings.
代码是有效的,但总是返回 false。通常由包含接受对象而不是泛型类型隐藏的错误,因此编译器仅限于警告。
Since there are valid use cases for passing an object, you should be able to use a @SuppressWarnings() annotation to hide this warning (only do this if you know what you are doing).
由于存在传递对象的有效用例,您应该能够使用 @SuppressWarnings() 注释来隐藏此警告(仅当您知道自己在做什么时才这样做)。