Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Lambdas & Functional Interfaces

Lesson 37 of 39 in the free Java notes on Siksha Sarovar, written by Rohit Jangra.

Why Lambdas?

Before Java 8, passing behaviour around meant writing verbose anonymous inner classes. Lambdas let you treat a single-method action as a value — shorter, clearer, and a better fit for collections + streams.

Lambda Syntax

(parameters) -> { body }

Zero parameters() -> System.out.println("Hi")One parameterx -> x * x (parens optional) • Many parameters(a, b) -> a + bBlock body(a, b) -> { int r = a + b; return r; }

Functional Interface

An interface with exactly one abstract method (SAM — Single Abstract Method). Optionally annotated with @FunctionalInterface so the compiler enforces the rule.

Java's java.util.function package ships the most common shapes:

InterfaceMethodUse
Runnablerun()No input, no output
Supplier<T>get()No input, returns T
Consumer<T>accept(T)Takes T, no output
Predicate<T>test(T)T → boolean
Function<T,R>apply(T)T → R
Comparator<T>compare(T,T)Sorting

Method References

A short-hand for a lambda that just calls an existing method.

list.forEach(System.out::println);     // Class::staticMethod  OR  instance::method
names.stream().map(String::toUpperCase);
list.sort(String::compareToIgnoreCase);

Variable Capture

A lambda can read final or effectively-final local variables. It cannot mutate them — that keeps closures predictable in multi-threaded code.