Java 8 Features – Lambda Expressions, Functional Interfaces, Streams, and More
Learn the powerful new features introduced in Java 8, including Lambda Expressions, Functional Interfaces, Streams API, Method References, and the new Date and Time API.
Java 8 introduced several powerful features that significantly improved productivity and enhanced functional programming capabilities in Java. These features help developers write concise, readable, and maintainable code. Let’s explore each of them.
1. Lambda Expressions
- Lambda expressions provide a clear and concise way to represent an instance of a functional interface.
- They allow you to pass behavior as arguments (i.e., passing code as a parameter).
Syntax: (parameters) -> expression
Example – Simple Lambda Expression
Key Points:
- Makes code more concise
- Great for functional interfaces (interfaces with only one abstract method)
2. Functional Interfaces
- An interface with only one abstract method is known as a functional interface.
- Java 8 provides the
@FunctionalInterfaceannotation to denote functional interfaces. - Examples of functional interfaces in the Java API include
Runnable,Callable,Comparator, etc.
Example – Custom Functional Interface
Key Points:
@FunctionalInterfaceensures that only one abstract method exists- Can be used as the target type for lambda expressions
3. Predicate, Function, Consumer, Supplier Interfaces
Java 8 introduced several built-in functional interfaces such as:
Predicate Interface
- Represents a boolean-valued function.
- Used for conditions and filtering.
Function Interface
- Represents a function that accepts one argument and produces a result.
Consumer Interface
- Represents an operation that accepts a single input and returns no result.
Supplier Interface
- Represents a function that supplies an object of a given type.
4. Streams API
- The Streams API allows you to process sequences of elements (like collections, arrays) in a functional style.
- Stream operations can be intermediate (e.g., filter, map) or terminal (e.g., collect, forEach).
Example – Filtering and Collecting Elements
Key Points:
- Stream API enables lazy evaluation
- Supports parallel operations using
parallelStream() - Intermediate and terminal operations
5. Method References
- Method references are shorthand for calling a method directly.
- Syntax:
ClassName::methodName
Example – Method Reference for Static Method
Key Points:
- Method references can be used for static methods, instance methods, and constructor references.
6. Optional Class
- The
Optionalclass is used to represent optional values that may or may not be present. - It is used to avoid NullPointerException.
Example – Using Optional
Key Points:
- Avoids null checks
- Useful for returning values that may be missing
7. Date and Time API (java.time)
- Java 8 introduced a new Date and Time API under
java.timepackage to improve the handling of dates, times, durations, and periods. - Provides immutable classes like
LocalDate,LocalTime,LocalDateTime,ZonedDateTime, andDuration.
Example – Using LocalDate and LocalTime
Key Points:
- Immutable classes for thread-safety
- Supports time zones, date-time arithmetic, and formatting
8. Default and Static Methods in Interfaces
- Java 8 introduced the ability to define default and static methods in interfaces.
Default Method:
- Provides a default implementation for methods in interfaces.
- Can be overridden in implementing classes.
Static Method:
- Can define static methods in interfaces which can be invoked without an instance of the class.
9. Summary
- Lambda Expressions provide concise, functional-style code.
- Functional Interfaces define single-method interfaces.
- Streams API makes it easier to handle sequences of data.
- Method References offer shorthand for method invocation.
- Optional helps avoid null-related issues.
- Date and Time API simplifies date and time operations.
- Default and Static Methods in Interfaces allow additional functionality in interfaces.