Skip to primary content
Java Technical Tutorial

Java Lambda Expressions, Method References & Functional Interfaces

Master Java lambda syntax: functional interfaces (@FunctionalInterface), Predicate, Function, Consumer, Supplier, and lexical scoping.

Java
import java.util.function.Predicate;
import java.util.function.Function;

public class LambdaGuide {
    public static void main(String[] args) {
        // Predicate: takes an input and returns a boolean
        Predicate<String> isValidPhone = phone -> phone != null && phone.matches("\\d{10}");
        System.out.println("Valid phone: " + isValidPhone.test("7503962162"));

        // Function: takes T and returns R
        Function<String, Integer> stringLength = String::length;
        System.out.println("Length: " + stringLength.apply("Ghaziabad"));
    }
}

1. What is a Lambda Expression?

A lambda expression is an anonymous block of code that provides an implementation of a Single Abstract Method (SAM) interface, commonly known as a Functional Interface. Introduced in Java 8, lambdas eliminate verbose anonymous inner classes.

2. Core Built-in Functional Interfaces

Java provides standard functional interfaces in java.util.function:

  • Predicate<T>: boolean test(T t) for filtering conditions.
  • Function<T, R>: R apply(T t) for mapping and transforming data.
  • Consumer<T>: void accept(T t) for operations with side effects.
  • Supplier<T>: T get() for deferred value generation.
Key Engineering Takeaways
  • Functional interfaces are marked with @FunctionalInterface.
  • Lambdas can capture effectively final variables from enclosing lexical scopes.
  • Method references provide clean shorthand syntax when a lambda simply forwards parameters to an existing method.

Frequently Asked Questions

Can a functional interface have default methods?
Yes! A functional interface can have multiple default and static methods, as long as it has exactly one abstract method.
Master This in Our Ghaziabad Coding Labs

Gain deep mastery over these concepts with direct instructor guidance, code reviews, and mock interviews at C-60 R.K Tower, 3rd Floor, RDC, Ghaziabad.

Explore Related Course Call 7503962162
Physical Training Center

Visit Our Ghaziabad Training Center

Join classroom batches, attend interactive lab sessions, code with mentors, and take advantage of our high-speed dev workstations.

Campus Address:
C-60 R.K Tower, 3rd Floor, RDC, Ghaziabad, Ghaziabad, Uttar Pradesh
Direct Counseling Helpline:
Call 7503962162
Get Google Maps Directions Call Now: 7503962162 Explore Ghaziabad Training Hub
Call 7503962162 Get Course Details