Java 8 Features with Examples

Here we will see Java 8 features in details.

 

Lambda expressions – It is an anonymous function which doesn’t have a name, access modifier and return type. .

Syntax –

  • (lambda-arguments)  – we can have one or multiple arguments or it can be empty.
  • ->(arrow) –     This  links (lambda-arguments) and {body}.
  • {lambda body}          – Here we have core logic.

Benefits of lambda expression –

  • Lambda expression allows us to use functional programming than object-oriented programming.
  • With help of lambda expression, we can reduce the code.
  • It supports parallel processing.
  • We can use predefined API and library in an easier way.

Note –

  • If we have only one line or statement in body curly braces are optional.
  • (lambda-arguments) – It can have any number of arguments.
  • If we use return keyword, curly braces are mandatory.
  • To call lambda expression we must use Functional interface(contains single abstract method ).
  • Functional interface can contain a single abstract method and any number of static or default method(these methods as we must have a body as we can have method body in the interface, Java 8 onwards).
  • Runnable and Comparable are interfaces are examples of the Functional interface.
  • We use @FunctionalInterface annotation to define a functional interface, although this is optional.

Let’s see a couple of examples related to the lambda expression.  All examples will be in both, earlier Java 8 and in Java 8.

  • Example 1 – Printing hello world using a lambda expression.

Without using lambda expression –

Output is – hello world

Using lambda expression –

Output is – hello world

In the above program, @FunctionalInterface is optional. But this annotation provides compile time safety if something goes wrong it will tell us. Even below code will work fine.

Output is – hello world

 

  • Example 2 – square of the number. Here we will have one parameter in the lambda expression

Without using lambda expression –

Output is – 100

Using lambda expression – square of two numbers

 

 

  • Example 3 – sum of two number. Here we will see program having two parameters in the lambda expression.

Without using lambda expression –

Output is – Sum of n1 and n2 is:- 50

Using lambda expression –

Output is – Sum of n1 and n2 is:- 50

  • Example 4 – In this example, we will see how to write return statement in the lambda expression.

Output is – 100

 

Example 5 –

Output is –

hello world
hello world

In the above program, what is the difference create references i1 and i11? In the case of line 20 Test class is implementing the I1 interface whereas line 23 we are creating a functional interface. Here i1 is a reference of I1 and object of Test class whereas i11 is a lambda expression type of I1.

Example 6 – Defining Functional interface as an inner class.

Output is –

600
RAM

Example 7 – 

Output is –

without lambda expression 0
without lambda expression 1
without lambda expression 2
without lambda expression 3
without lambda expression 4
with lambda expression 0
with lambda expression 1
with lambda expression 2
with lambda expression 3
with lambda expression 4

Example 8 – 

Output is – true

Example 9 – 

Output is –

Using enhanced for loop ram
Using enhanced for loop mohan
Using enhanced for loop sohan
Using lambda expression ram
Using lambda expression mohan
Using lambda expression sohan

Example 10 – 

Output –

Sorting book object on basis of name
bookId is 6 bookName is devdash
bookId is 2 bookName is five point someone
bookId is 4 bookName is godan
bookId is 1 bookName is lolita
sorting book object on basis of id
bookId is 1 bookName is lolita
bookId is 2 bookName is five point someone
bookId is 4 bookName is godan
bookId is 6 bookName is devdash

 

Example 11 – 

Output is –

value of i is 0
value of i is 1
value of i is 2
value of i is 3
value of i is 4

Example 12 – 

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit

Top