Govt. Exams
Entrance Exams
Predicate<T> is a functional interface that takes a single input of type T and returns a boolean. It's commonly used for filtering operations in streams.
The syntax (x, y,) is incorrect because there's a trailing comma after y. The correct syntax would be (x, y) -> x + y.
@FunctionalInterface is the standard annotation introduced in Java 8 to explicitly mark an interface as a functional interface. It helps in compile-time checking.
A functional interface must have exactly one abstract method. This is the defining characteristic that allows it to be used with lambda expressions and method references.
Supplier greeting = () -> "Hello";
System.out.println(greeting.get());
Supplier.get() returns the value produced by the lambda expression, which is "Hello".
A functional interface must have exactly one abstract method, which can be implemented using lambda expressions.
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
UnaryOperator applies the operation x * x where x = 5, resulting in 25.
BiFunction add = (a, b) -> a + b;
System.out.println(add.apply(5, 3));
BiFunction takes two Integer parameters and returns their sum. apply(5, 3) returns 5 + 3 = 8.
The expression uses comparison operator (>), which returns a boolean value.
Lambda expressions can only be assigned to functional interfaces that have exactly one abstract method.