Fundamentals 9 min read

Understanding Java 8 Lambda Expressions: From Anonymous Classes to Functional Interfaces

This article explains Java 8 lambda expressions by revisiting anonymous classes, introducing functional interfaces, showing step‑by‑step code conversions, and outlining syntax shortcuts and best‑practice guidelines for writing concise and readable lambda code.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java 8 Lambda Expressions: From Anonymous Classes to Functional Interfaces

Java 8 introduces lambda expressions as a concise way to create instances of functional interfaces, replacing the verbose anonymous class pattern that has been used since JDK 1.1.

Historically, developers used anonymous classes to implement single‑method interfaces such as Comparator for sorting:

public interface Comparator
{
    int compare(T o1, T o2);
}
Collections.sort(words, new Comparator
() {
    public int compare(String s1, String s2) {
        return Integer.compare(s1.length(), s2.length());
    }
});

While functional, this approach is verbose. Java 8 treats interfaces with exactly one abstract method as *functional interfaces* and allows them to be instantiated with lambda expressions:

Collections.sort(words, (s1, s2) -> Integer.compare(s1.length(), s2.length()));

To illustrate the transition, the article defines a simple functional interface Learn and shows three ways to provide its implementation.

@FunctionalInterface
public interface Learn {
    void study();
}
public class StudyDemo implements Learn {
    @Override
    public void study() {
        System.out.println("好好学习,天天向上");
    }
}
Learn s = new StudyDemo();
 s.study(); // prints 好好学习,天天向上

Using an anonymous inner class:

Learn s = new Learn() {
    @Override
    public void study() {
        System.out.println("好好学习,天天向上");
    }
};
 s.study();

And finally with a lambda expression, first in block form and then in the most compact form:

Learn s = () -> {
    System.out.println("好好学习,天天向上");
};
 s.study();
Learn s = () -> System.out.println("好好学习,天天向上");
 s.study();

The article also covers lambdas with parameters and return values. For an interface with two parameters:

interface Learn1 {
    void study(int a, int b);
}
Learn1 learn = (a, b) -> System.out.println("好好学习x" + (a + b));
learn.study(3, 4);

When there is a single parameter, parentheses can be omitted, and if the body consists of a single statement, both braces and the return keyword can be omitted:

learn = e -> System.out.println("好好学习" + e);
learn.study(5);

For a real‑world example, the article defines a Student class and sorts an array of students by age using both an anonymous comparator and its lambda equivalents:

public class Student {
    private String name;
    private int age;
    public Student(String name, int age) { this.name = name; this.age = age; }
    public int getAge() { return age; }
}

Student[] array = {
    new Student("张三", 18),
    new Student("李四", 20),
    new Student("王五", 19)
};

Comparator
compare = new Comparator
() {
    @Override
    public int compare(Student s1, Student s2) {
        return s1.getAge() - s2.getAge();
    }
};
Arrays.sort(array, compare);

Lambda version (full syntax):

Comparator
compare = (Student s1, Student s2) -> {
    return s1.getAge() - s2.getAge();
};
Arrays.sort(array, compare);

Compact lambda version:

Comparator
compare = (s1, s2) -> s1.getAge() - s2.getAge();
Arrays.sort(array, compare);

Key guidelines highlighted include:

A lambda can only be used with a functional interface (exactly one abstract method).

Type inference works when the target type is known from the context.

Parentheses around parameters can be omitted for a single parameter, and braces and return can be omitted for a single statement.

Keep lambdas short—ideally one line; longer lambdas should be refactored for readability.

In summary, Java 8 lambda expressions provide a clear, concise way to represent small function objects, and developers should prefer them over anonymous classes whenever possible.

JavaProgramminglambdaFunctional InterfaceJava8anonymous-class
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.