不兼容的类型:Predicate<CAP#1> 无法转换为 Predicate<?超级帽#2>
incompatible types: Predicate<CAP#1> cannot be converted to Predicate<? super CAP#2>
这是我的代码。
import java.util.stream.Stream;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;
public class StreamMethod{
public static void main(String... args){
List<Integer> data = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8));
allMatch(data, x -> x < 10);
}
public static <T> void allMatch(List<?> list, Predicate<? super T> predicate){
boolean allSatisfy = list.stream().allMatch(predicate);
System.out.println(allSatisfy);
}
}
我遇到了错误 incompatible types: Predicate<CAP#1> cannot be converted to Predicate<? super CAP#2>
。你能帮我解决吗?为什么会显示此错误?
那我也把上面的传参行改成
Predicate<Integer> pred= x -> x < 10;
allMatch(data, pred);
但还是报错
您应该改为 List<T> list
而不是 List<?> list
这是我的代码。
import java.util.stream.Stream;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;
public class StreamMethod{
public static void main(String... args){
List<Integer> data = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8));
allMatch(data, x -> x < 10);
}
public static <T> void allMatch(List<?> list, Predicate<? super T> predicate){
boolean allSatisfy = list.stream().allMatch(predicate);
System.out.println(allSatisfy);
}
}
我遇到了错误 incompatible types: Predicate<CAP#1> cannot be converted to Predicate<? super CAP#2>
。你能帮我解决吗?为什么会显示此错误?
那我也把上面的传参行改成
Predicate<Integer> pred= x -> x < 10;
allMatch(data, pred);
但还是报错
您应该改为 List<T> list
而不是 List<?> list