Govt. Exams
Entrance Exams
The map() with lambda x -> x * 2 doubles each element: 1*2=2, 2*2=4, 3*2=6, 4*2=8, resulting in [2, 4, 6, 8].
filter() method in streams takes Predicate which tests condition and returns boolean to determine if element should be included.
Supplier<T> takes no parameters () and returns T. Perfect for generating/providing values like random numbers.
Lambda can have multiple statements using braces: (x,y) -> { int z = x+y; return z; }. Single expression needs no braces or return.
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
Lambdas provide concise syntax, are compiled directly without separate class files, and work only with functional interfaces (single abstract method).
BiFunction takes 2 parameters and returns a result. (int a, int b) -> a + b fits this. Others are Function, Supplier, Consumer respectively.
Lambda expressions can access local variables only if they are final or effectively final (not modified after initialization) due to closure requirements.
List list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(x -> System.out.print(x + " "));
forEach with Consumer lambda prints each element followed by space, resulting in '1 2 3 4 5 ' (with trailing space).
Predicate<T> tests a condition and returns boolean. It's ideal for filtering. Function would return any type, Consumer doesn't return anything.