Govt Exams
BiFunction concat = (a, b) -> a + b;
concat.apply("Java", "8", "Features");
BiFunction takes exactly 2 parameters. Passing 3 arguments is compilation error. apply() only accepts 2 arguments.
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.
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".
@FunctionalInterface is optional annotation that helps compiler verify interface has exactly one abstract method, catching errors early.
Function add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
Curried function: add.apply(3) returns a Function that adds 3. .apply(5) on that adds 5 to 3, returning 8.
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).