← Back to Questions
Java

Lambda expressions and Stream API examples

Learn Lambda expressions and Stream API examples with simple explanations, real-time examples, interview tips and practical use cases.

Java 8 Lambda Expressions and Stream API

Introduction

Java 8 introduced Lambda Expressions and the Stream API, revolutionizing how developers write code. These features brought functional programming concepts into Java, enabling concise, expressive, and efficient data processing.

Lambda Expressions

Syntax and Basics


List names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
  

Functional Interfaces

Lambdas work with functional interfaces like Predicate, Function, Consumer, and Supplier.

Examples


// Using Predicate
Predicate isEven = n -> n % 2 == 0;
System.out.println(isEven.test(4)); // true

// Using Comparator
List list = Arrays.asList("banana", "apple", "cherry");
list.sort((a, b) -> a.compareTo(b));
System.out.println(list);
  

Advanced Lambda Usage

  • Closures and variable capture
  • Method references
  • Constructor references

Stream API

Introduction

Streams provide a declarative way to process collections. They support operations like filtering, mapping, reducing, and can run in parallel.

Stream Creation


Stream stream = Stream.of("a", "b", "c");
List list = Arrays.asList("x", "y", "z");
Stream listStream = list.stream();
  

Intermediate Operations

  • filter()
  • map()
  • flatMap()
  • distinct()
  • sorted()
  • peek()

Terminal Operations

  • forEach()
  • collect()
  • reduce()
  • count()
  • findFirst()
  • findAny()

Examples


List numbers = Arrays.asList(1,2,3,4,5);
int sum = numbers.stream()
                 .filter(n -> n % 2 == 0)
                 .mapToInt(n -> n)
                 .sum();
System.out.println(sum); // 6
  

Collectors

Collectors provide powerful ways to group, partition, and summarize data from streams.


Map> grouped = 
    names.stream().collect(Collectors.groupingBy(String::length));
System.out.println(grouped);
  

Parallel Streams

Streams can run in parallel to leverage multi-core processors. However, they must be used carefully to avoid performance pitfalls.

Performance Analysis

Lambdas and streams improve readability but may introduce overhead. Benchmarking is essential to optimize performance.

Interview-Ready Notes

  • Q: Difference between streams and collections?
    A: Collections store data; streams process data.
  • Q: Why Optional?
    A: To avoid null checks and NullPointerException.
  • Q: Lambda vs anonymous class?
    A: Lambdas are concise and focus on behavior, anonymous classes are verbose.
  • Q: What is CompletableFuture?
    A: A class for async programming with chaining and combining tasks.

Conclusion

Lambda Expressions and the Stream API transformed Java programming by introducing functional programming concepts. Mastering these features is essential for writing efficient, maintainable, and modern Java applications.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.