Doing More with Less: Java 8 Lambda Expressions
Using lambda expressions can make your Java code easier to read, stronger, and more readable.
One of the criticisms of a programming language is the number of instructions written to perform a specific processing, and sometimes you have to write a lot of code. Lambda expressions have solved this problem for a few cases and make Java stronger and easier, as C# has been the case for years.
Lambda expressions are new in Java SE 8, and these are the most notable features added to this release. Lambda expressions provide a clearer way to represent the methods and interfaces that use these expressions. Lambda expressions also improve Java object collections by making it easier to browse lists, filter and extract data from collections. Performance features strengthen Java in multi-tasking environments available on all new modern computers.
Programmers are afraid that lambda expressions will pollute the object-oriented nature of Java with the concept of functional programming. In fact, these are the same criticisms that affected the C# language a few years ago, but it has become better than before.
LINQ is a set of features, also introduced in Visual Studio 2008, that allowed the ability to create queries to be added. LINQ is useful for querying SQL database, XML documents, ADO.Net dataset and .Net collections, files, strings, etc.
LINQ and lambda expressions have become popular in the C# community. So, it is also possible that this will be the case for the Java.
Interfaces that define only one method are called functional interfaces in java 8. Lambda expressions are often used with functional interfaces and internal classes.
Let's consider these three lambda expressions:
One of the criticisms of a programming language is the number of instructions written to perform a specific processing, and sometimes you have to write a lot of code. Lambda expressions have solved this problem for a few cases and make Java stronger and easier, as C# has been the case for years.
Lambda expressions are new in Java SE 8, and these are the most notable features added to this release. Lambda expressions provide a clearer way to represent the methods and interfaces that use these expressions. Lambda expressions also improve Java object collections by making it easier to browse lists, filter and extract data from collections. Performance features strengthen Java in multi-tasking environments available on all new modern computers.
Programmers are afraid that lambda expressions will pollute the object-oriented nature of Java with the concept of functional programming. In fact, these are the same criticisms that affected the C# language a few years ago, but it has become better than before.
Lambda and LINQ expressions in C# and VisualBasic.Net
Lambda expressions were added to C# and VB.Net, primarily to support LINQ (Language-Integrated Query). Lambda expressions are anonymous functions that you can use to create tree-like expressions. In C#, you can create a lambda expression by specifying the input parameters in the Side left of the lambda operator =>, and put the expression or block of instructions on the other side. For example, the expression x=> x*x specifies a parameter named x and returns the square of x. In VB.Net. In this case, you use anonymous functions to create lambda.LINQ is a set of features, also introduced in Visual Studio 2008, that allowed the ability to create queries to be added. LINQ is useful for querying SQL database, XML documents, ADO.Net dataset and .Net collections, files, strings, etc.
LINQ and lambda expressions have become popular in the C# community. So, it is also possible that this will be the case for the Java.
Anonymous inner classes in Java
Anonymous inner classes in Java were the first step towards lambda expressions. You can use them by calling the inner class as shown in the following example:JButton button = new JButton("test");The inner class ActionListener added to the button is an inner class that contains a method actionPerformed() in it. This saves us a bit of code, but not too much.
button.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent e){
System.out.println("one click");
}
});
Interfaces that define only one method are called functional interfaces in java 8. Lambda expressions are often used with functional interfaces and internal classes.
Lambda Expression Syntax in Java
The lambda operator in Java is -> . Like C#, Java 8 attaches arguments as shown in this example:(int x) -> x * xWe declared a variable x of integer type and we want to return the square of this variable x.
Let's consider these three lambda expressions:
(int x, int y) -> x + yNow consider the example ActionListener previous with lambda:
() -> 36
(String s) -> { System.out.println(s); }
JButton button = new JButton("test");It is clear that this code is smaller and more readable. We also notice that the inner class ActionListener became the argument e.
testButton.addActionListener(e -> System.out.println("one click"));
The java.util.function
The java.util.function was added in Java 8 and contains five types of functional interfaces:- Predicate: A property of the passed object as a parameter.
- Consumer: An action that should be triggered with the passed object as an argument.
- Function: Renders a single result Y: X-> Y
- Provider: Contains an instance of X
- Unary Operator: of X-> x
- Binary operator of (X, X)-> x
Object collections in Java 8
Lambda expressions can improve the use of collections in java. With the different versions, features have been added to the collections that combine very well with lambda expressions, such as the forEach set on both interfaces Iterator and Iterable.
Example:
We want to define a List which contains the list of students and this class has as its attribute the age of the student.
Listand = Student.createShortList();
Now we can define a predicate to be able to select from List.
PredicateFinally, we can do something with our selection:list = and -> et.getAge() >= 20;
aClass.doqlqthing(pl, list);This is much faster than previous versions of Java. If you want to do a more complex process, it helps to use the forEach():
et.forEach( e -> e.getName() );As a conclusion, the features added to the Java language will not weaken its object-oriented concept in any way. These features will make the language strong, rich, and elegant.
References: