Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.function.BinaryOperator;
- // A BinaryOperator Represents an operation upon two operands of the same type, producing a result of the same type
- // as the operands. This interface contains one method "apply".
- /**
- * Class that implements BinaryOperator<Boolean> to perform the "or" operation on two booleans.
- */
- class Or implements BinaryOperator<Boolean>{
- @Override
- public Boolean apply(Boolean b1, Boolean b2) {
- return b1 || b2;
- }
- }
- public class Main {
- // 1 -------------------
- // Using a fully defined class "Or" (see above)
- BinaryOperator<Boolean> or = new Or();
- // 2 -------------------
- // Using an anonymous class (No class name; looks like you're making an instance of this interface. In this case you
- // must implement all methods of the interface in the body:
- BinaryOperator<Boolean> implies = new BinaryOperator<Boolean>() {
- @Override
- public Boolean apply(Boolean b1, Boolean b2) {
- return !b1 || b2;
- }
- };
- // 3 -------------------
- // Using a lambda expression to implement an instance of this interface is possible for any Functional Interface.
- // A functional interface is an interface with just one abstract method. See the @FunctionalInterface annotation
- // in the definition of BinaryOperator; this prevents the interface from defining more than one method.
- BinaryOperator<Boolean> and = (b1, b2) -> b1 && b2;
- }
Add Comment
Please, Sign In to add comment