Consider the following code:
public class Main {
private static Predicate<? extends TestObject> predicate = testObject -> true;
private static Predicate<? extends TestObject> predicate1 = testObject -> true;
public static void main( String[] args ) {
List<TestObject> objects = Lists.newArrayList( new TestObject(), new TestObject() );
objects.stream().filter( predicate.or( predicate1 ) ).findFirst();
}
}
It doesn't compile, giving the error:
Error:(17, 48) java: incompatible types: java.util.function.Predicate<capture#1 of ? extends test.test.TestObject> cannot be converted to java.util.function.Predicate<? super capture#2 of ? extends test.test.TestObject>
It seems like we can't join such predicates with logical operators like "or" or "and", but why can't Java deal with them?
It compiles with simple predicates like Predicate<TestObject>, but not with Predicate<? super TestObject> or Predicate<? extends TestObject>.