Java > Java 8 Features > Lambda Expressions > Using Built-in Functional Interfaces
Lambda Expressions with Built-in Functional Interfaces: Example 2 - Function
This snippet demonstrates how to use a lambda expression with the `Function` functional interface, a built-in interface in Java. `Function` is a functional interface that accepts one argument and produces a result. It's commonly used to transform data.
Code Example
This code showcases how to use the `Function` functional interface with lambda expressions to transform a list of strings. The `toUpperCase` function converts each name to uppercase, and the `nameLength` function calculates the length of each name. The `map` method of the `Stream` API accepts a `Function` and applies it to each element in the stream, returning a new stream with the transformed elements.
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FunctionExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Using Function to convert names to uppercase
Function<String, String> toUpperCase = name -> name.toUpperCase();
List<String> upperCaseNames = names.stream()
.map(toUpperCase)
.collect(Collectors.toList());
System.out.println("Uppercase names: " + upperCaseNames); // Output: [ALICE, BOB, CHARLIE]
// Using Function to get the length of each name
Function<String, Integer> nameLength = name -> name.length();
List<Integer> nameLengths = names.stream()
.map(nameLength)
.collect(Collectors.toList());
System.out.println("Name lengths: " + nameLengths); // Output: [5, 3, 7]
}
}
Concepts Behind the Snippet
This snippet utilizes the `Function` functional interface. The `Function` interface has the `apply(T t)` method, which accepts an argument of type `T` and returns a result of type `R`. Lambda expressions provide a concise way to implement this abstract method.
Real-Life Use Case
Consider a scenario where you need to process a list of product objects and extract specific information, such as the product name or price. You can use `Function` with lambda expressions to define transformations that extract the desired data from each product object, creating a new list of transformed values.
Best Practices
Interview Tip
Be prepared to discuss the differences between `Function`, `Consumer`, `Predicate`, and `Supplier` functional interfaces. Understand their respective use cases and provide examples of how to use them with lambda expressions. Explain how these interfaces contribute to functional programming in Java.
When to Use Them
Use `Function` when you need to transform one type of object into another type. This is useful for data mapping, data conversion, and extracting specific information from objects.
Alternatives
Before Java 8, transformations were typically handled using loops and custom methods. The `Function` interface, paired with lambda expressions, offers a more elegant and functional approach. Anonymous inner classes could also be used, but they are more verbose.
Pros
Cons
FAQ
-
What is the purpose of the `Function` interface?
The `Function` interface represents a function that accepts one argument and produces a result. It is commonly used for transforming data. -
What are the input and output types of the `Function` interface?
The `Function` interface takes a generic type `T` as input and returns a generic type `R` as output. `T` is the type of the input argument, and `R` is the type of the result. -
How does the `map` method work with the `Function` interface?
The `map` method of the `Stream` API applies the `Function` to each element in the stream and returns a new stream containing the transformed elements.