猿问

Java 8 Predicate - 为什么不能加入通配符泛型谓词?

考虑以下代码:


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();

    }

}

它不编译,给出错误:


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>

似乎我们不能将这样的谓词与诸如“或”或“与”之类的逻辑运算符连接起来,但是为什么 Java 不能处理它们呢?


它使用简单的谓词(如 )进行编译Predicate<TestObject>,但不使用Predicate<? super TestObject>or 进行编译Predicate<? extends TestObject>。


宝慕林4294392
浏览 224回答 3
3回答

胡说叔叔

您可能知道谓词是兼容的,但编译器不兼容。想象一下这个例子:Predicate<? extends Collection<Object>> p1 = (Set<Object> s) -> s.isEmpty();Predicate<? extends Collection<Object>> p2 = (List<Object> l) -> l.get(0) != null;我们开发人员可以看到,第一个谓词在技术上可以处理所有集合,而第二个谓词只能处理列表。但是想象一下谓词在其他地方被初始化或者同时被改变。编译器无法确定谓词对象是为哪种类型的集合创建的。因此,您根本无法使用它们:Set<Object> set = new HashSet<>();List<Object> list = new ArrayList<>();p1.test(set);p2.test(list);p1.test(list);p2.test(set);所有这些调用都不会编译,因为编译器无法确定背后的实际对象p1和p2can 是否完全适用于这些类型的集合。这就是意思? extends Collection<>:你知道它是一个特定的子类型,但你不能告诉编译器究竟是哪一个。用一个更简单的例子来说明这一点:Collection<Apple> appleBasket = ...;appleBasket.add(new Apple());&nbsp; // worksappleBasket.add(new Orange()); // does not work (obviously)Collection<Fruit> mixedFruitBasket = ...;mixedFruitBasket.add(new Apple());&nbsp; // worksmixedFruitBasket.add(new Orange()); // works// Now the tricky partCollection<? extends Fruit> unknownButPureFruitBasket = ...;unknownButPureFruitBasket.add(new Apple());&nbsp; // does not work&nbsp;unknownButPureFruitBasket.add(new Orange()); // does not work您不能将任何一种水果添加到您不知道其类型的篮子中。事实上,它可能是一个可以容纳所有水果的篮子,但也可能是一个纯苹果篮子、橙色篮子,甚至是你还不知道的香蕉篮子。在您的 IDE 中尝试:List<? extends String> l1 = new ArrayList<>();List<? extends String> l2 = new ArrayList<>();l1.addAll(l2);Eclipse 告诉我:类型 List<capture# 1 -of 中的addAll(Collection1-of ? extends String>) 方法 extends String> 不适用于参数 (List<capture# 2 -of ? extends String>)请注意不同的类型:addAll期望是 的集合capture#1,l2是 的集合capture#2。

aluckdog

filter需要一个Predicate<? super TestObject>,而不是Predicate<? extends TestObject>。为什么super TestObject?因为这里的 TestObject 是一个输入参数,一个消费者。根据PECS规则,它应该被标记为super。Predicate<? extends TestObject>完全不同于Predicate<? super TestObject>. 前者可以接受TestObjectand 的所有子类TestObject。后者可以接受的所有超TestObject和TestObject。显然,它们是不同的且不兼容的。

白猪掌柜的

这实际上与 Java 如何处理谓词无关,而只是泛型问题。简而言之,答案是predicate和predicate1不一定兼容。参数的类型or为Predicate<? super T>,假设T在两个Predicate对象中是相同的具体类型参数。但这种情况并非如此。为了说明问题,假设:class TestObject2 extends TestObject {&nbsp; &nbsp; boolean isTrue() {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}class TestObject3 extends TestObject {&nbsp; &nbsp; boolean isTrue3() {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}并且(注意声明的类型没有改变,但实际的 lambda 表达式改变了):private static Predicate<? extends TestObject> predicate =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (TestObject2 testObject) -> testObject.isTrue();private static Predicate<? extends TestObject> predicate1 =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; (TestObject3 testObject) -> testObject.isTrue3();在这个例子中,or会得到错误的值(predicate接受一个TestObject2对象,但在这种情况下,它可以用一个TestObject3对象调用)。你认为predicate1这会如何工作filter?Stream.of(new TestObject2(), new TestObject2()).filter(predicate.or(predicate1));事实是,编译器低保Predicate<? extends TestObject>是既兼容Predicate<TestObject2>和Predicate<TestObject3>(这是按照仿制药的法律)
随时随地看视频慕课网APP

相关分类

Java
我要回答