Lambda Expressions in Java 8

Previous
Next

Java 8 has introduced a new feature called Lambda expressions.It is considered to be a major change in java. As this change will bring functional programming into java. Other languages such as Scala already have this feature so this is not new to programming world, it is new to java
Before understanding Lambda expressions, Lets first understand Functional Interface.

What is Functional Interface?

Functional interfaces are those interfaces which have only one abstract method in it. It can have more than one default or static method and can override method from java.lang.object. Lets create a functional interface:
Java can itself identify Functional Interface but you can also denote interface as Functional Interface by annotating it with @FunctionalInterface.
Some popular Functional Interfaces are:
  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.awt.event.ActionListener
  • java.util.Comparator

Why Lambda Expressions?

Lets understand with help of  Anonymous Comparator:
The problem with Anonymous Comparator is of syntax. Each time you want to sort list using comparator, you have to remember bulky syntax.
So generally main problem with Anonymous classes are syntax.For very simple operation, we need to write complex code. To solve this problem, JDK has introducenew feature called Lambda Expressions.I will take this example after explaining lambda expression to understand , how lambda expression will reduce this complex code.

What is Lambda Expressions:

lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body .You can call it function without name,
Connection between Lambda Expression and Functional Interface:
You might be thinking I have introduced functional Interface above but how it is connected to Lambda.So Lambda expression can be applied for abstract method of functional Interface which is being implemented or being instantiated anonymously.

Structure of Lambda Expressions

So we can divide structure of Lambda expression to three parts:
    1. Argument list or parameters
      • Lambda expression can have zero or more arguments.
      • You can choose to not declare type of arguments as it can be inferred from context.

      • you can not declare one argument’s type and do not declare type for other argument.

    • When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses
  1. Array token (->)
  2. Body
    • Body can have expression or statements.
    • If there is only one statement in body,curly brace is not needed and return type of the anonymous function is same as of  body expression
    • If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.
Lets take some examples ,  you want to initialize thread and now you will realize, how can lambda expression can make your coding easy :
Lets take another example of Comparator which we have seen in Anonymous Comparator in java.
Create a class called Country.java
Create a main class called ComparatorMain.java
As you can see here, we have used lambda expression for using Comparator. SO inspite of wrting Anonymous comparator, our expression became very easy.
So we have passed 2 arguments o1 and o2, we didn’t pass type because it can inferred from context.
We have only one statement here, so no need to put it in curly braces.

Lets create HelloWorld Lambda Expression Example:

Create an Interface Called HelloWorld:
Create a class called HelloWorldMain
Run above program you will get following output:
Hope you understand Lambda expression now, in next post, we will see more examples of it.

Java 8 interview questions

In this post, we will see some more java 8 interview questions. I will try to cover logical questions rather …
Read More

Java 8 tutorial

In this post, we will see about Java 8 tutorial. I have written a lot of tutorials on Java 8 …
Read More

Java 8 Optional

In this post, we will see about Java 8 optional. Did you ever get NullPointerException as Java developer? If you …
Read More

Java 8 Parallel Stream

In this post, we will see about Parallel Stream in java. Java 8 introduces the concept of parallel stream to …
Read More

Java 8 – java.util.function.Function example

In this post, we will see about java.util.function.Function functional interface. java.util.function.Function is part of java.util.function package. There are some inbuilt functional interfaces …
Read More

Java Stream collect to array

In this post, we will see how to collect any java 8 Stream to array. There are many ways to …
Read More

Difference between map and flatMap in java

In this post, we will see the difference between map and flatMap in java.I have already covered Stream’s map and …
Read More

Java 8 Stream flatMap

In this post, we will see about Java 8 Stream flatMap function.Before understanding flatMap, you should go through stream’s map …
Read More

Java 8 Stream Map

In this post, we will see about Java 8 Stream map function. Stream’s map method takes single element from input …
Read More
Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog