Hello, dear Jetto Net forum followers!
We continue our series on Java object-oriented programming (OOP). In the previous article, we examined the powerful tool that Java provides for storing, organizing, and managing data: the Collections Framework. In this article, we will look at Lambda expressions and functional interfaces, introduced in Java 8, which make code more readable and functional.
What Are Lambda Expressions?
Lambda expressions are a short and concise way to express methods in Java. Lambda expressions are also known as anonymous functions because they do not need to have a name. These expressions typically represent small code blocks aimed at a single purpose. Their general structure is as follows:
(parameter list) -> { expression or code block }
Let's write a lambda expression that performs addition:
- In the example above, the lambda expression takes two integer parameters. (int a, int b): This is the parameter list of the lambda expression.
- ->: This is the lambda operator. It separates the parameter list from the expression or code block.
- { return a + b; }: This is the body of the lambda expression. In the example above, the lambda expression adds the two parameters and returns the result.
What Are Functional Interfaces?
Functional interfaces are interfaces that have only one abstract method. Lambda expressions are used to implement the abstract methods of functional interfaces. This way, lambda expressions can be used through functional interfaces.
With Java 8, many ready-to-use functional interfaces were added in the java.util.function package. Some of the most commonly used functional interfaces are:
- Predicate<T>: Takes an object of type T and returns a boolean value.
- Consumer<T>: Takes an object of type T and does not return any value.
- Function<T, R>: Takes an object of type T and returns an object of type R.
- Supplier<T>: Takes no parameters and returns an object of type T.
Lambda Expressions and Method References
Instead of using lambda expressions, we can also reference an existing method to achieve the same functionality. This method is called a method reference. Method references make the code even more readable.
// Lambda expression
list.forEach(x -> System.out.println(x));
// Method reference
list.forEach(System.out::println);
Functional Operations on Collections with the Stream API
The Stream API, introduced with Java 8, allows us to perform functional operations on collections. Lambda expressions and method references can be used with the Stream API to perform operations like filtering, sorting, and transforming collections more easily and readably.
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evenNumbers = numbers .stream()
.filter(x -> x % 2 == 0)
.collect(Collectors.toList());
In the example above, we filtered an integer list to create a new list that contains only even numbers.
Conclusion
Lambda expressions and functional interfaces are important features that bring the power of functional programming to Java. By using these features, we can make our code more readable, concise, and flexible.
In our next article, we will continue exploring other important concepts of OOP. Feel free to ask your questions or share your thoughts in the comments section.
Happy Coding!