Introduction To Lambda Expressions

What is a Lambda?

  • A Lambda is a Function
  • A function is a computation that takes parameters and returns a value
  • Until Java8 , function could only be implemented using methods
  • A lambda enables functions to be passed around or stored like data (which enables to pass lambdas as arguments)

Example: Sorting a List of Objects

let's consider the following class Person where the attributes are name and age:

public class Person
{

  private String name;
  private Long age;


  public Person(String name, Long age)
  {
    this.name = name;
    this.age = age;
  }

  public String getName()
  {
    return name;
  }
}
List<Person> persons= new ArrayList<Person>();

Let's consider that we want to sort this person, then we will use the method  sort of the class Collections as following:

Collections.sort(persons,???); // the second parameter of sort method corresponds to how we want to sort this list

Let's consider that we want to sort it by name, then, naturaly we want to give  as parameter to this method "a function" which describes how we want to sort this List.

But for that we need to define the following class which implements a Comparator that wrap a Person ....

class PersonComparator implements Comparator<Person>
{

  public int compare(Person o1, Person o2)
  {
    return o1.getName().compareTo(o2.getName());
  }
}

Afterwards, we pass an instance of this PersonComparator class to the sort method as following:

 

Collections.sort(persons,new PersonComparator());

In order to test that, create a Main class as following:


package com.company;

/**
 *  * Created by @MHI on 22/04/2016.
 *  
 */

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

public class Main
{


  public static void main(String[] args)
  {
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("alpha", 25L));
    persons.add(new Person("beta", 29L));
    persons.add(new Person("gamma", 22L));
    persons.add(new Person("zeta", 18L));

    class PersonComparator implements Comparator<Person>
    {
      public int compare(Person o1, Person o2)
      {
        return o1.getName().compareTo(o2.getName());
      }
    }
    Collections.sort(persons, new PersonComparator());

    for (Person lPerson : persons)
    {
      System.out.println(lPerson.getName());
    }


  }
}

Introductiontolambdas1

It works but we can notice two heavy inconvinients:

  • we are creating an instance of an object just in order to use a method, which has a very bad impact on the memory usage
  • it is very long and redundant  to write ( but we can inline the creation of the instance of the comparator within the sort method)

 

Step By Step To Lambdas

As we said previously we can inline the construction of PersonComparator class (which will be used as an anonymous class) as following:

Collections.sort(persons, new Comparator<Person>()
{
  public int compare(Person o1, Person o2)
  {
    return o1.getName().compareTo(o2.getName());
  }
});

we can remark that the function that we give as a parameter to the sort method is Bold written one, in other words we naturaly want to write it like that (But this code won't compile)

Collections.sort(persons, (Person o1, Person o2)
{
  return o1.getName().compareTo(o2.getName());
}
);

Then, tune it a little bit and you will get your first Lambda:

Collections.sort(persons,(Person o1, Person o2)-> o1.getName().compareTo(o2.getName()));

 

 

Add a comment