October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Programming with Lambdas in Eclipse Luna

  • August 6, 2014
  • By Manoj Debnath
  • Send Email »
  • More Articles »

With the inception of Eclipse Luna, Java developers get a full-featured, Java 8-compliant IDE to try their hand at lambda expression. However, Eclipse has been supporting Java 8 or at least some part of it even with its recent predecessor. Luna is fresh born as a complete Java 8-compliant IDE, so when it comes to using lambda expression, we can definitely choose Luna, although it is not the only choice. In this article, our main focus will be on lambda expression and its use in our regular programming and the IDE of our choice will obviously will be Eclipse Luna.

Basic Syntactic Structure and Rules of Lambda

While working with lambda, you'll find the following basic syntactic structure of writing an expression.

1. (zero/more parameter) -> <expression>;

There can be zero or more parameters where the type of parameters can be explicitly declared or inferred from the context. If there is a single expression, there is no need to enclose it within curly braces.

2. (zero/more parameter) -> { <statement 1>; <statement 2>; ...;}

When there are multiple statements, they should be enclosed within curly braces. Although there can be zero/more statements and braces, in fact braces serve the same purpose as we use in normal Java coding.

The (arrow) -> token acts as a separator between the set of parameters and the actual expression. Observe that, in any lambda expression, there is a lot going on behind the scene, such as inferring the type of parameters and calling the right function. This heavy lifting is done by the compiler itself and not by the JVM. The operator (::) is used to reference methods. This operator is introduced with Java 8 and used on many occasions along with the arrow (->) operator in lambda expression. We shall see this later in the article.

Where does Lambda Expression Fit?

First of all, there is no hard and fast rule that we must apply lambda expression in Java code. We can get the same desirable effect even if we write classical Java code; it is "classical" in the sense that no lambda expression is used.

Secondly, there are code segments that can be best defined through lambda expression. If we go the classical way, things looks messy and less maintainable. Analogically, it reminds me of the use of recursive function in programming—some problems are inherently recursive, but recursion is not the only solution. The same is true for lambda.

Thirdly, to find where we may or must apply lambda expression lies in the context of coding. Lambda expression never came out of the blue; the concept was always there (lambda calculus). Java 8 is reborn especially with this flagship change. Therefore, to get a hint of where we can apply lambda expression rests in the context of the following: anonymous classes, method references, functional interfaces, method calls, and so on.

Perhaps, we can get an idea of its usage if we run through the following example.

Classical Way: The Way We Do

Suppose there is an Employee class:

 public class Employee {
      public enum Type {
           PERMANENT, CONTRACTUAL, COMMISSIONED, TRAINEE
      };
      private String name;
      private Type category;
      private LocalDate birthDate;
 
      //...constructors, getters, and setters
 }   

We want to find the list of employees whose age is more than 40 years, so we can do this:

      public static List<Employee> filterSeniorEmployees(List<Employee>employees){
           List<Employee> list=new ArrayList<>();
           for(Employee emp: employees){
                Period p=Period.between(emp.getBirthDate(), LocalDate.now());
                if(p.getYears()>40)
                     list.add(emp);
                }
                  return list;
      }
   

Next, say we want to obtain the list of employees whose category id is PERMANENT. Again, we have to filter with the help of the following code.

public static List<Employee> filterPermanentEmployees(List<Employee> employees){
          List<Employee> list=newArrayList<>();
          for(Employee emp:employees){
               if(emp.getCategory()==Employee.Type.PERMANENT)
                    list.add(emp);
          }
          return list;
     }

Modifying Further: The Way It can be Done

According to the software engineering principle, the preceding example is obviously is not a very good way of programming in view of the code duplicity. The coding of both the filters is kind of similar. Java 8 provides a way out; we can pass these conditions as an argument and use the filter methods without code duplication, as follows.

public class Employee {

     //... 

     public static boolean isPermanentEmployee(Employee emp){
          return Employee.Type.PERMANENT==emp.getCategory();
     }

     public static boolean isSeniorEmployee(Employee emp){
          Period p=Period.between(emp.getBirthDate(), LocalDate.now());
          return p.getYears()>=40;
     }
}


public class MyLambdaTest {
	
     public interface MyPredicate<T>{
          public boolean test(T t;
     }

     public static List<Employee> filterEmployees(List<Employee>      MyPredicate<Employee> p){
          List<Employee> list=new ArrayList<>();
          for(Employee 
emp:employees){
               if(p.test(emp))
                    list.add(emp);
          }
          return list;
     }

     public static void main(String[] args) {

          List<Employee> employees=new ArrayList<>();

          filterEmployees(employees, Employee::isPermanentEmployee);
          // or
          //filterEmployees(employees, Employee::isSeniorEmployee);

     }

}

Using Lambda Expression

We can further improve the preceding code with the help of lambda (anonymous function) that enables us to write:

public class Employee {

     //... 
}


public class MyLambdaTest

     public interface MyPredicate<T>{
          public boolean test(T t);
     }

     public static List<Employee> filterEmployees(List<Employee> employees,      MyPredicate<Employee> p){
          List<Employee> list=new ArrayList<>();
          for(Employee emp:employees){
               if(p.test(emp))
                    list.add(emp);
          }
          return list;
     }

     public static void main(String[] args) {

     filterEmployees(employees,
          (Employee e) -> Employee.Type.PERMANENT == e.getCategory());

     filterEmployees(employees, (Employee e) -> {
          boolean result = false;
          Period period = Period.betweene.getBirthDate(), LocalDate.now());
          if (period.getYears() > 40)
               result = true;
          return result;
          });

          }
}

Once we use lambda, there is no use of the function isSeniorEmployee and isPermanentEmployee and we do not need them in the Employee class any more. We can define our functional requirement while we call filterEmployees. Observe that the test method of MyPredicate is implicitly called once we define our requirement in filterEmployees using lambda expression.

Conclusion

Lambda expression can be very powerful when used effectively. It can reduce duplicate code to a great extent. The problem is that older codes and libraries need to be revamped with lambda. This could be a strenuous job. Lambda has added another chapter in the core Java framework. The intricacies of its application can be overcome with better understanding of its concept. The ultimate rule of productivity can be leveraged with delicate handling and appropriate fusion of lambda in existing Java code. Eclipse Luna as a container for implementing this chemistry was highly sought in the developer community as one of the most trusted IDEs.


Tags: Java, Eclipse, IDE, Java 8, lambda expressions




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel