bad return type in lambda expression

后端 未结 2 1135
情深已故
情深已故 2021-02-05 14:00

The following code compiles fine in IntelliJ and Eclipse, but the JDK compiler 1.8.0_25 complains. First, the code.

import java.util.function.Predicate;

public          


        
2条回答
  •  无人及你
    2021-02-05 14:17

    includes Object, which isn't a Boolean. ie

    MyPredicate
    

    Could be a

    MyPredicate
    
    
    

    and you can't return an Object as a boolean.

    Try changing your lambda to:

    MyStream. create().filter(b -> Boolean.TRUE.equals(b));
    

    which will compile and execute without error not matter the type of the stream, but will return true for elements that are true Boolean values.

    提交回复
    热议问题