Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

incompatible types: Predicate<CAP#1> cannot be converted to Predicate<? super CAP#2>

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.

like image 500
Asif Mushtaq Avatar asked Oct 30 '22 00:10

Asif Mushtaq


1 Answers

You should change to List<T> list instead of List<?> list

like image 113
Eugene Avatar answered Nov 17 '22 08:11

Eugene