Lambda expressions tutorial In java 8




1) Introduction to Lambda expressions?
Lambda expressions are the most valuable addition in java 8. So, we will discuss about them in detail with easy and very simple examples.


2) In short about Lambda expression In java 8 >
  • we can write lambda expression to replace the anonymous inner class.
  • lambda expression make code very neat and clean.
  • lambda expression are very to read. So, they make code more readable.


3) Before learning Lambda expression you must know about >
  • functional interface and
  • anonymous inner class. So, just take a very quick look at both of these.


In short about functional interface. Read about functional interface in detail.


In short about anonymous inner class. Read about anonymous inner class in detail.


Example 1 > How to use Lambda expression


Example 1.1 > Before Java 8 -  Sort String using Using Local class - Without Lambda expression >
import java.util.Arrays;
import java.util.Comparator;
public class SortStringArrayWithoutLambdaExpressionExample {
     
   public static void main(String... args) {
       String[] stringArray = {"ab", "ef", "cd"};
      
       //Create Local class
       class StringSort implements Comparator<String> {
           public int compare(String a, String b) {
                return a.compareTo(b);
          }
       }
    
       //Before Java 8 -  Sort String using Using Local class - Without Lambda expression
       System.out.println("Before Java 8 - Sort StringArray using Using "
        + " > Local class - i.e. Without Lambda expression");
       Arrays.sort(stringArray, new StringSort());
      
       //Display StringArray
         for (String str : stringArray) {
              System.out.print(