下面是intellij抛出Suspicious collections method calls
警告的代码片段,但我不明白为什么。我唯一能想到的是,也许intellij认为其中一个列表可能为空,但这也会引发相同的错误。
这是一个Intellij错误,还是真的有某个角落的情况,我没有想到?
public class Foo {
public static void main(String[] args) {
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
bar.remove(foo); // Warning: 'List<String>' may not contain objects of type 'List<String>'
}
}
public class Foo {
public static void main(String[] args) {
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
if (foo != null && bar !=null) {
bar.remove(foo); // Warning: 'List<String>' may not contain objects of type 'List<String>'
}
}
}
Intellij版本2022.1.4终极版
发布于 2022-08-09 19:45:14
List<String> foo = Arrays.asList("a", "b", "c");
List<String> bar = new ArrayList<>(foo);
// bar.remove(foo); This is the same thing as:
bar.remove(Arrays.asList("a", "b", "c")); // still makes no sense.
// What would make sense:
bar.remove("a"); // remove the element "a"
bar.removeAll(foo); // remove all the elements in foo
简而言之,在List<String>
中,您通常会调用remove(String)
或removeAll(Collection<String>)
,而不是remove(List<String>)
,后者不会真正做您想做的事情。
发布于 2022-08-09 20:07:29
List<String> bar = new ArrayList<>(foo);
构造包含foo元素的列表,而不是foo本身,
https://stackoverflow.com/questions/73297257
复制相似问题