Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Java 8 - Part 1 Lambda, FnInterface, MethodRef

Uploaded by

amaleshmsc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Java 8 - Part 1 Lambda, FnInterface, MethodRef

Uploaded by

amaleshmsc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Java Lambda Expressions

Lambda expression is a new and important feature of Java which was included in
JavaSE 8. It provides a clear and concise way to represent one method interface
using an expression. It is very useful in collection library. It helps to iterate, filter
and extract data from collection.
The Lambda expression is used to provide the implementation of an interface which has
functional interface. It saves a lot of code. In case of lambda expression, we don't need to
define the method again for providing the implementation
Java lambda expression is treated as a function, so compiler does not create .class file.

Functional Interface
Lambda expression provides implementation of functional interface. An interface
which has only one abstract method is called functional interface. Java
provides an anotation@FunctionalInterface, which is used to declare an interface
as functional interface.
Why use Lambda Expression
To provide the implementation of Functional interface.
Less coding.

Java Lambda Expression Syntax


(argument-list) -> {body}
Java lambda expression is consisted of three components.
Argument-list: It can be empty or non-empty as well.
Arrow-token: It is used to link arguments-list and body of expression.
Body: It contains expressions and statements for lambda expression.
No Parameter Syntax
() -> {
//Body of no parameter lambda
}
One Parameter Syntax
(p1) -> {
//Body of single parameter lambda
}
Two Parameter Syntax
(p1,p2) -> {
//Body of multiple parameter lambda
}
Without Lambda Expression

interface Drawable{
public void draw();
}
public class LambdaExpressionExample {
public static void main(String[] args) {
int width=10;

//without lambda, Drawable implementation using anonymous class


Drawable d=new Drawable(){
public void draw(){System.out.println("Drawing "+width);}
};
d.draw();
}
}
Test it Now

Output:
Drawing 10

Java Lambda Expression Example


Now, we are going to implement the above example with the help of Java lambda
expression.
@FunctionalInterface //It is optional
interface Drawable{
public void draw();
}

public class LambdaExpressionExample2 {


public static void main(String[] args) {
int width=10;

//with lambda
Drawable d2=()->{
System.out.println("Drawing "+width);
};
d2.draw();
}
}
Output:
Drawing 10

Java Lambda Expression Example: with or without return


keyword
In Java lambda expression, if there is only one statement, you may or may not use
return keyword. You must use return keyword when lambda expression
contains multiple statements.
interface Addable{
int add(int a,int b);
}

public class LambdaExpressionExample6 {


public static void main(String[] args) {

// Lambda expression without return keyword.


Addable ad1=(a,b)->(a+b);
System.out.println(ad1.add(10,20));

// Lambda expression with return keyword.


Addable ad2=(int a,int b)->{
return (a+b);
};
System.out.println(ad2.add(100,200));
}
}
Test it Now

Output:
30
300

Java Lambda Expression Example: Foreach Loop

for (String name : names) {


System.out.println(name);
}
We can write this using forEach:
names.forEach(name -> {
System.out.println(name);
});

import java.util.*;
public class LambdaExpressionExample7{
public static void main(String[] args) {

List<String> list=new ArrayList<String>();


list.add("ankit");
list.add("mayank");
list.add("irfan");
list.add("jai");

list.forEach(
(n)->System.out.println(n)
);
}
}
Output:
ankit
mayank
irfan
jai

Java Lambda Expression Example: Multiple Statements

@FunctionalInterface
interface Sayable{
String say(String message);
}

public class LambdaExpressionExample8{


public static void main(String[] args) {
// You can pass multiple statements in lambda expression
Sayable person = (message)-> {
String str1 = "I would like to say, ";
String str2 = str1 + message;
return str2;
};
System.out.println(person.say("time is precious."));
}
}
Output:
I would like to say, time is precious.

Java Lambda Expression Example: Creating Thread


You can use lambda expression to run thread. In the following example, we are
implementing run method by using lambda expression.
public class LambdaExpressionExample9{
public static void main(String[] args) {

//Thread Example without lambda


Runnable r1=new Runnable(){
public void run(){
System.out.println("Thread1 is running...");
}
};
Thread t1=new Thread(r1);
t1.start();
//Thread Example with lambda
Runnable r2=()->{
System.out.println("Thread2 is running...");
};
Thread t2=new Thread(r2);
t2.start();
}
}
Test it Now

Output:
Thread1 is running...
Thread2 is running...
Java Functional Interfaces
An Interface that contains exactly one abstract method is known as functional
interface. It can have any number of default, static methods but can contain only one
abstract method. It can also declare methods of object class.
Functional Interface is also known as Single Abstract Method Interfaces or SAM
Interfaces. It is a new feature in Java, which helps to achieve functional programming
approach.
Example 1
@FunctionalInterface
interface sayable{
void say(String msg);
}
public class FunctionalInterfaceExample implements sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample fie = new FunctionalInterfaceExample();
fie.say("Hello there");
}
}
Output:
Hello there

A functional interface can have methods of object class. See in the following
example.
Example 2

@FunctionalInterface
interface sayable{
void say(String msg); // abstract method
// It can contain any number of Object class methods.
int hashCode();
String toString();
boolean equals(Object obj);
}
public class FunctionalInterfaceExample2 implements sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample2 fie = new FunctionalInterfaceExample2();
fie.say("Hello there");
}
}
Test it Now

Output:
Hello there

Invalid Functional Interface


A functional interface can extends another interface only when it does not have any
abstract method.
interface sayable{
void say(String msg); // abstract method
}
@FunctionalInterface
interface Doable extends sayable{
// Invalid '@FunctionalInterface' annotation; Doable is not a functional interface
void doIt();
}
Output:
compile-time error

Example 3
In the following example, a functional interface is extending to a non-functional
interface.
interface Doable{
default void doIt(){
System.out.println("Do it now");
}
}
@FunctionalInterface
interface Sayable extends Doable{
void say(String msg); // abstract method
}
public class FunctionalInterfaceExample3 implements Sayable{
public void say(String msg){
System.out.println(msg);
}
public static void main(String[] args) {
FunctionalInterfaceExample3 fie = new FunctionalInterfaceExample3();
fie.say("Hello there");
fie.doIt();
}
}
Output:
Hello there
Do it now

@FunctionalInterface
interface A
{
intadd(inta);
}

public class LampdaFnInt{

public static void main(String args[]) {


A obj= a->
{
System.out.println("passed value is "+a);
return a;
};

System.out.println(obj.add(1000));
}

Java Predefined-Functional Interfaces


Java provides predefined functional interfaces to deal with functional programming by
using lambda and method references.
You can also define your own custom functional interface. Following is the list of
functional interface which are placed in java.util.function package.

- Predicate -Biprtedicatetest()
- Function - Bifunctionaccept()
- Consumer - Biconsumerapply()
- Supplier - Bisupplierget()

- import java.util.function.*;

public class Predefinded{

static intcalculate()
{
inth=100+200;
return h;
}
public static void main(String args[])
{
Predicate<Integer>ref=(s)->(s>-10)&&(s<10);
booleanresult=ref.test(41);
System.out.println(result);

Predicate<Integer>obj=a->(a>100);
booleanresult1=obj.test(6789);
System.out.println(result1);

BiPredicate<Integer,Integer>obj1=(b,c)->(b>c);
booleanresult2=obj1.test(102,20);
System.out.println(result2);

BiPredicate<String,String>obj2=(s1,s2)->s1.equals(s2);
booleanresult3=obj2.test("appless","apple");
System.out.println(result3);

Function<String,Integer>aa= f->f.length();
System.out.println(aa.apply("i love india"));

Consumer<String>bb=s->
{
System.out.println(s.concat("goodboy"));
};
//System.out.println(bb.accept("kesaraj"));
bb.accept("kesavraj");

Supplier<Integer>ss=()->Predefinded.calculate();
System.out.println(ss.get());
}
}

Static Instance:
package Java8.Functionalinterface;
@FunctionalInterface
interface McD{
void burger();
default void ketchUp() {
System.out.println("Ketchup given");
}
static void cheese() {
System.out.println("Cheese given");
}
}

// With Lambda
public class DefaultStaticMethods// implements McD
{

public static void main(String[] args) {


// TODO Auto-generated method stub
McD ref = ()->System.out.println("Burger given");
ref.burger();
ref.ketchUp();
McD.cheese();

Jg j=new Jg();
j.burger();
}

// Without Lambda
class Jgimplements McD{
public void burger() {
System.out.println("hi..");
ketchUp();
McD.cheese();
System.out.println("---------------------");

}
}

Java Method References


Java provides a new feature called method reference in Java 8. Method reference is
used to refer method of functional interface. It is compact and easy form of lambda
expression. Each time when you are using lambda expression to just referring a
method, you can replace your lambda expression with method reference.
Types of Method References
There are following types of method references in java:
Reference to a static method.
Reference to an instance method.
Reference to a constructor.

1) Reference to a Static Method


You can refer to static method defined in the class. Following is the syntax and example
which describe the process of referring static method in Java.
Syntax
1. ContainingClass::staticMethodName
Example 1
In the following example, we have defined a functional interface and referring a static
method to it's functional method say().
1. interface Sayable{
2. void say();
3. }
4. public class MethodReference {
5. public static void saySomething(){
6. System.out.println("Hello, this is static method.");
7. }
8. public static void main(String[] args) {
9. // Referring static method
10. Sayable sayable = MethodReference::saySomething;
11. // Calling interface method
12. sayable.say();
13. }
14.}
Test it Now

Output:
Hello, this is static method.

Example 2
In the following example, we are using predefined functional interface Runnable to refer
static method.
1. public class MethodReference2 {
2. public static void ThreadStatus(){
3. System.out.println("Thread is running...");
4. }
5. public static void main(String[] args) {
6. Thread t2=new Thread(MethodReference2::ThreadStatus);
7. t2.start();
8. }
9. }
Test it Now

Output:
Thread is running...

Example 3
You can also use predefined functional interface to refer methods. In the following
example, we are using BiFunction interface and using it's apply() method.
1. import java.util.function.BiFunction;
2. class Arithmetic{
3. public static int add(int a, int b){
4. return a+b;
5. }
6. }
7. public class MethodReference3 {
8. public static void main(String[] args) {
9. BiFunction<Integer, Integer, Integer>adder = Arithmetic::add;
10.int result = adder.apply(10, 20);
11.System.out.println(result);
12.}
13.}
Test it Now

Output:
30
Example 4
You can also override static methods by referring methods. In the following example, we
have defined and overloaded three add methods.
1. import java.util.function.BiFunction;
2. class Arithmetic{
3. public static int add(int a, int b){
4. return a+b;
5. }
6. public static float add(int a, float b){
7. return a+b;
8. }
9. public static float add(float a, float b){
10.return a+b;
11.}
12.}
13.public class MethodReference4 {
14.public static void main(String[] args) {
15.BiFunction<Integer, Integer, Integer>adder1 = Arithmetic::add;
16.BiFunction<Integer, Float, Float>adder2 = Arithmetic::add;
17.BiFunction<Float, Float, Float>adder3 = Arithmetic::add;
18.int result1 = adder1.apply(10, 20);
19.float result2 = adder2.apply(10, 20.0f);
20.float result3 = adder3.apply(10.0f, 20.0f);
21.System.out.println(result1);
22.System.out.println(result2);
23.System.out.println(result3);
24.}
25.}
Test it Now

Output:
30
30.0
30.0

2) Reference to an Instance Method


like static methods, you can refer instance methods also. In the following example, we
are describing the process of referring the instance method.
Syntax
1. containingObject::instanceMethodName
Example 1
In the following example, we are referring non-static methods. You can refer methods by
class object and anonymous object.
1. interface Sayable{
2. void say();
3. }
4. public class InstanceMethodReference {
5. public void saySomething(){
6. System.out.println("Hello, this is non-static method.");
7. }
8. public static void main(String[] args) {
9. InstanceMethodReference methodReference = new InstanceMethodReference(); // Creating
object
10. // Referring non-static method using reference
11. Sayable sayable = methodReference::saySomething;
12. // Calling interface method
13. sayable.say();
14. // Referring non-static method using anonymous object
15. Sayable sayable2 = new InstanceMethodReference()::saySomething; // You can use anon
ymous object also
16. // Calling interface method
17. sayable2.say();
18. }
19.}
Test it Now

Output:
Hello, this is non-static method.
Hello, this is non-static method.

Example 2
In the following example, we are referring instance (non-static) method. Runnable
interface contains only one abstract method. So, we can use it as functional interface.
1. public class InstanceMethodReference2 {
2. public void printnMsg(){
3. System.out.println("Hello, this is instance method");
4. }
5. public static void main(String[] args) {
6. Thread t2=new Thread(new InstanceMethodReference2()::printnMsg);
7. t2.start();
8. }
9. }
Test it Now

Output:
Hello, this is instance method

Example 3
In the following example, we are using BiFunction interface. It is a predefined interface
and contains a functional method apply(). Here, we are referring add method to apply
method.
1. import java.util.function.BiFunction;
2. class Arithmetic{
3. public int add(int a, int b){
4. return a+b;
5. }
6. }
7. public class InstanceMethodReference3 {
8. public static void main(String[] args) {
9. BiFunction<Integer, Integer, Integer>adder = new Arithmetic()::add;
10.int result = adder.apply(10, 20);
11.System.out.println(result);
12.}
13.}
Test it Now

Output:
30

3) Reference to a Constructor
You can refer a constructor by using the new keyword. Here, we are referring constructor
with the help of functional interface.
Syntax
1. ClassName::new
Example
1. interface Messageable{
2. Message getMessage(String msg);
3. }
4. class Message{
5. Message(String msg){
6. System.out.print(msg);
7. }
8. }
9. public class ConstructorReference {
10. public static void main(String[] args) {
11. Messageable hello = Message::new;
12. hello.getMessage("Hello");
13. }
14.}
Test it Now
ADVERTISEMENT

Output:
Hello

Instance method

package Java8.MethodRef;

interface A
{
void disp(inta);
//void disp1(int a);
}

public class InstanceMethod{


private intdisplay(inta)
{
System.out.println("hi ");
return 100;
}
public static void disp(inta)
{
System.out.println("welcome");
}

public static void main(String[] args) {

InstanceMethodim=new InstanceMethod();
A ref=im::display;
ref.disp(56);
A ref1=InstanceMethod::disp;
ref1.disp(20);

}
Constructor using

Exe-1
package Java8.MethodRef;

interface A5{
void aa(); // mention the void as return type for multiple classes
}

class B1{
B1(){
System.out.println("B..");
}
}

class C1{
C1(){
System.out.println("C..");
}
}

public class MultiClassConstructor{

public static void main(String[] args) {


// TODO Auto-generated method stub
A5 obj= C1::new;
obj.aa();
A5 obj1=B1::new;
obj1.aa();
}

exe-2

package Java8.MethodRef;

interface A3{
B print(); //mention the class name as return type
}

class B{
B(){
System.out.println("B..");
}
}

public class ConstructorDifferentClass{

public static void main(String[] args) {


// TODO Auto-generated method stub
A3 obj= B::new;
obj.print();
}

You might also like