Java: Functional Programming f(x) – Part2

In the previous article we discussed about the importance of Functional Programming, Lambda Calculus etc.

In this article, we further discuss about important features of Functional Programming like

1. Lambda Expressions
2. Method References
3. Functional Interfaces
4. Streams API

And we will discuss about the relationship between each of the features.

Lambda Expressions

JSR 335 has greatly facilitated programming in a multicore environment by introducing Lambda Expressions/closures/anonymous method, and related features.

If Lambda Expressions had been integrated into the Collections API from the beginning, their development could have taken a different path. However, they have enriched the Collections API by introducing new methods to current interfaces and introducing new Interfaces such as ‘Stream’.

Why Lambda Expressions?

Before the introduction of Lambda Expressions, developers commonly used ubiquitous anonymous inner classes, which were more verbose and overwhelming.

However, Lambda expressions are simplified, made more concise, and made it elegant and concise way to write a block of code that was previously written in an anonymous class.

So,

What is Lambda Expression?

According to Oracle documentation, a lambda expression is just a shorter way of writing an implementation of a method for later execution.

At compile time, the lambda expression determines its type (variable, field, method, or return type of a method) and it must be a functional interface. And you cannot write a lambda expression for an anonymous class that does not implement a functional interface.

After we determine the lambda expression’s type from the functional interface class, we can conveniently locate the appropriate method, namely the abstract method, for the implementation. As a result, the lambda expression only executes the abstract method from the functional interface.

Composing a lambda expression involves three components:

() -> { }

A block of parameters
An Arrow and Java uses meager arrows(->)
Providing a block of code that serves as the method’s body

Nevertheless, we can streamline the syntax from () -> {} to argument -> block of code which gives more concise, simple, and readable. We can omit () if you have one argument.

For example, for the below list, we can convert the anonymous inner class syntax which takes too many lines to express the basic concept.

List<String> names = List.of(“Foojay”, “Java”, “Steve”, “Mahi”);

Collections.sort(names, new Comparator<String>() {

@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}

});

Using the lamda expression, we can simplify the above code snippet like the below,

List<String> names = List.of(“Foojay”, “Java”, “Steve”, “Mahi”);

names.sort((str1, str2) -> str1.compareTo(str2));

The provided code snippet describes the lambda expression and sorting method used for the list of names as follows:

Type of Lambda Expression:

The lambda expression (str1, str2) -> str1.compareTo(str2) serves as a Comparator lambda expression. It defines the comparison logic used for sorting the elements in the list.
This lambda expression is of type Comparator, which is a functional interface in the java.util package.

Method Used for Implementation:

The list is sorted using the method List.sort(Comparator c). This method takes a Comparator as an argument to establish the order of the elements.

The post Java: Functional Programming f(x) – Part2 appeared first on foojay.