Govt. Exams
Entrance Exams
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.
All three are valid lambda expression syntaxes with different parameter types and return statements.
map() is the intermediate operation used to transform elements from one type/value to another. It applies a function to each element.
@FunctionalInterface is a compile-time check that ensures an interface has exactly one abstract method, making it safe for lambda implementation.
Method references are compact lambda expressions that directly reference existing methods. They use :: syntax and are equivalent to lambdas that call those methods.
BiFunction<T, U, R> represents a function that accepts two arguments of types T and U, and returns a result of type R.