Home > Java > Java 8 : Lambda Example

Java 8 : Lambda Example

Lambda Expressions:

Lambda expressions are new and important feature of Java 8. They provide a clear way to represent one method interface using lambda expression. And they provide easy way to access Collection libraries for iterating, filter and extract data.

Example :

Syntax : (arg1, arg2) -> {body}

For Ex:


(int a, int b) -> {  return a + b; }

() -> System.out.println("Hello World");

(String s) -> { System.out.println(s); }

//Old way of iterating Collection

List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);

for(Integer n: list) {

System.out.println(n);

}

//New way with lambda:

List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);

list.forEach(n -> System.out.println(n));

Rules:

  1. It can have zero or more number of arguments.
  2. Parameters are separated by using comma
  3. Empty parenthesis are used to represent empty parameters eg : () -> System.out.println(“Hello World”);
  4. Curly braces for body is not mandatory if it contains only single statement, other wise it should enclosed with curly braces.

Program:


interface TestInterface{
void display();
}
public class TestInterfaceImpl{

public static void main(String[] args){
TestInterface li = new TestInterface(){
public void display(){
System.out.println("Hello World");
}
};
li.display();
}
}

Here we used anonymous class to implement TestInterface and this interface has only one method, by using lambda expression without using anonymous class we can call the display() method


interface TestInterface{
void display();
}
public class TestInterfaceImpl{

public static void main(String[] args){
//Old way:
TestInterface old = new TestInterface(){
public void display(){
System.out.println("Old : Display method called!");
}
};
old.display();
//New way with Lambda expression
TestInterface li = () -> System.out.println("New : Display method called !");
li.display();

}
}

This approach we regularly use in Java Swing, while adding an action to Swing Component we use ActionListener actionPerformed() method, ActionListener  is an interface which has only one method called actionPerformed(),


//old way:
button.addActionListener(new ActionListener()){
public void actionPerformed(ActionEvent e){
System.out.println("The button was clicked using old fashion code!");
}
}
//New way:
button.addActionListener( (e) -> {
System.out.println("The button was clicked. From lambda expressions !");
});

java.util.Runnable also has only one method public void run(), this also we can represent using lambda


//old way

Runnable r = new Runnable(){

public void run(){

System.out.println("Runnable run method");

}

}

//New way with lambda expression

Runnable r = ()->System.out.println("Runnable run method");

//So we can create Thread object like below

Thread t = new Thread(()->{System.out.println("Runnable run method from lambda")});

Any interface which has only one method can be used with lambda expression instead of old anonymous inner class.

Thanks for reading.

Advertisements
Categories: Java
  1. Hitesh Bargujar
    June 23, 2014 at 12:32 am

    can you please explain what is Lambda Expression is simple English so that it is easy to learn the concept first

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: