Java Lambda Expressions: Write Less, Do More

Unleash the Power of Lambdas, Function Interface, Consumer, and Predicate in Java

Java Lambda Expressions: Write Less, Do More

Lambda Expressions

Lambda expressions, introduced in Java 8, are a concise way of expressing anonymous functions. Lambda expressions allow you to treat behavior as data, making it easier to pass it as arguments to methods or store it in variables. They were added in Java 8 to help with functional programming in the language.

Lambda expressions consist of three main components:

  1. Parameters: These are the lambda expression’s input arguments. They can have a value of zero or more and are specified within parentheses. When there is only one parameter, the brackets are not required. Even if there are no parameters, you must use empty brackets.

  2. Arrow Operator (->): This detaches the parameters from the lambda expression’s body. It’s called the “arrow operator” because it resembles an arrow pointing from the parameters to the body.

  3. Body: This code will be run when the lambda expression is called. It can be a single statement or a block of code surrounded by curly braces. If it’s a single expression, the lambda’s return value is the expression’s value. If it’s a block of code, you can specify the return value explicitly with a return statement.

First, we’ll write a simple Java program that doesn’t use a Lambda expression. It’s right here:

Number increment :: 15
String concatenation :: Hello World!!!

The preceding code is an example of a traditional Java program in which the HelloWorlds class implements two interfaces: IIncrement and IConcatenate. It exemplifies imperative programming.

We will now rewrite the preceding code using the Lambda expression:

Number increment :: 15
String concatenation :: Hello World!!!

We have two interfaces: IIncrement and IConcatenate, which have abstract methods increment(int a) and concatenate(String a, String b). We used the Lambda expression to create instances of these interfaces within the main method. In contrast to the previous program, the HelloWorld class does not implement the interfaces. We have only provided the implementation of the interfaces’ methods here.

Here’s another example of how to use the lambda expression to create a Thread.

Sum of even numbers :: 20
Sum of odd numbers :: 25

Functional Interface

The concept of a functional interface is closely related to the concept of functional programming, which emphasizes writing programs with pure functions and treating functions as first-class citizens. A functional interface has only one abstract method, which is known as the functional method.

To define a functional interface, you must use the @FunctionalInterface annotation, which is optional. This annotation is not required, but using it ensures that the interface has only one abstract method. The compiler will generate an error if the interface contains more than one abstract method.

In the previous Lambda expression example, we have interfaces with only one abstract method. Lambda expressions are frequently used with functional interfaces. The context in which the lambda expression is used determines its type, so it is critical to use functional interfaces that match the lambda’s signature.

In Java 8, the java.util.function package added several built-in functional interfaces to cover various common use cases, such as Consumer, Supplier, Predicate, Function, and so on. These interfaces have already been annotated with @FunctionalInterface and can be used with lambda expressions directly.

Function Interface

It denotes a function that accepts one argument and returns a result. It takes two arguments: input and output. This interface has several functional methods, including apply(object), andThen(object), and compose(object). Now consider the Function interface example.

Is 64 even number :: true 
FUNCTIONAL PROGRAMING 
FUNCTION Interface 
FUNCTION INTERFACE

The apply() method is used to invoke the function, which means that the lambda expression contained within the function will be executed. The apply() method is also used to pass a value to a specific function.

The andThen() and compose() methods connect multiple functions to achieve a specific result.

When we use the andThen() method to connect two functions, the parameterized function that we passed into andThen() is executed after the first function. So, in this case, upperCaseFunction() will run first, followed by concatFunction().

In the case of the compose() method, the parameterized function that we passed in will be executed first, followed by the first function. So concatFunction() will be executed first, followed by upperCaseFunction().

Movie List

We’ve written a static method that returns a list of Movies. This movie list is used later in the tutorial. Here is our MovieRecord and MovieRecordList class.

Consumer Interface

The java.util.function package contains the Consumer interface. Java 8 introduced the Consumer interface. It’s used in Java to implement functional programming. It denotes a function that takes a single argument and returns a result. It has the following functional methods: accept() and andThen(). Consider the following example of a consumer interface:

The accept() method operates on the given argument. The andThen() method returns a Consumer that performs this operation first, followed by the after operation.

BiConsumer Interface

The BiConsumer interface is included in the java.util.function package. The BiConsumer interface has been available since Java 8. It is used to implement functional programming in Java. It represents a function that takes two arguments and returns a result. It is similar to the Consumer interface, except that it takes two arguments. Consider the following example:

Predicate Interface

Predicate is Java 8 functional interfaces. A predicate is a function with a single argument that returns true or false. The test() method of Predicate returns true or false.

A predicate is a simple statement that determines whether the opinions are true or false based on arguments.

It has the following functional methods: test(), and(), or() and negate(). Consider the following example:

The test() method evaluates the predicate on the given argument. The and() method returns a composed predicate representing a logical AND between two predicates. or() method returns a composed predicate representing a short-circuiting logical OR between two predicates. The negate() method returns a predicate representing the logical inverse of this predicate.

BiPredicate Interface

BiPredicate is a functional interface in Java 8. It is similar to the Predicate interface except that it accepts two arguments. Consider the following example:


Happy coding!!!

Originally published atcodenicetomedear.blogspot.com.

Did you find this article valuable?

Support Arijit Sarkar by becoming a sponsor. Any amount is appreciated!