Lambda Expression – Java 8 Feature

0
1049

Java 8 has introduced many new features and addition of Lambda Expressions is the most popular, powerful and useful one.

A lambda expression is like a method which provides a list of parameters and a body (an expression or a block of code) expressed in terms of those parameters.

It’s a shorthand that allows you to write a method in the same place you are going to use it. It comes handy especially in places where a method is being used only once, and the method definition is short. In turn, it saves the effort of declaring and writing a separate method to the containing class.

Lambda Expressions Syntax

The basic syntax of a lambda expression is:


(parameters) ->expression               
OR
(parameters) ->{ statements; }

Lets have a quick look at few examples of Java lambda expressions:

  1. (String str) -> System.out.print(str)       // prints the string str to console
  2. a -> 10 * a                        // takes number a and returns the result as 10*a
  3. (int a, int b) -> a + b        // takes two integers and returns their sum

package com.gb.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Java8LambdaExpression {

public static void main(String args[]){
 List<String> stringSet1 = new  ArrayList<String>();
 
 stringSet1.add("Hello");
 stringSet1.add("World");
 stringSet1.add("Lambda ");
 stringSet1.add("Expressions");
 stringSet1.add("Code");

 List<String> stringSet2 = new ArrayList<String>(stringSet1);

 Java8LambdaExpression simpleTest = new Java8LambdaExpression();

 System.out.print("Hello World - I am Sorting Strings using Traditional Way :");
 simpleTest.sortingInJava7(stringSet1);
 System.out.println(stringSet1);

 System.out.print("Hello World - I am Sorting Strings using Java 8 Lambda Expressions :");
 simpleTest.sortingInJava8(stringSet2);
 System.out.println(stringSet2);
}

//sorting in Java 7

private void sortingInJava7(List<String> names){

Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
}

//sorting in Java 8
private void sortingInJava8(List<String> stringSet){

 Collections.sort(stringsSet, (s1, s2) -> s1.compareTo(s2));
}
}

Output of the above code :

Hello World – I am Sorting Strings using Traditional Way :[Code, Expressions, Hello, Lambda , World]

Hello World – I am Sorting Strings using Java 8 Lambda Expressions :[Code, Expressions, Hello, Lambda , World]

Previous articleWhy is change Difficult?
Next articleforEach – Java 8 Feature
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY