Govt. Exams
Entrance Exams
The lambda expression prints the length of each string. Lengths are 1, 2, and 3 respectively, with spaces between them.
Lambda expressions reduce boilerplate code by allowing inline implementation of functional interfaces without creating anonymous inner classes, enabling a more functional programming style.
Supplier<T> is a functional interface with method get() that takes no parameters and returns a value of type T.
forEach with the lambda expression n -> System.out.println(n) iterates through each element and prints it with a newline.
The map() method in Stream API returns a new Stream with transformed elements of type R, maintaining the stream pipeline.
Lambda expressions can only access local variables that are final or effectively final to ensure thread safety and immutability.
Consumer print = s -> System.out.println(s.toUpperCase());
print.accept("java");
Consumer accepts one argument and returns nothing. print.accept("java") calls lambda which prints s.toUpperCase() = "JAVA".
UnaryOperator square = x -> x * x;
System.out.println(square.apply(5));
UnaryOperator applies a function on its argument and returns result of same type. square.apply(5) returns 5*5 = 25.
The java.util.function package contains functional interfaces like Predicate, Consumer, Function, Supplier, and BiFunction introduced in Java 8.
A functional interface has exactly one abstract method. It can have multiple default methods. The @FunctionalInterface annotation can be used to mark it.