Here is my code.
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);
}
}
I'm getting the error incompatible types: Predicate<CAP#1> cannot be converted to Predicate<? super CAP#2>
. Can you help me to solve it? and why it is showing this error?
Then I also change the above lines of passing arguments as
Predicate<Integer> pred= x -> x < 10;
allMatch(data, pred);
But still facing the error.
You should change to List<T> list
instead of List<?> list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With