Recently, Java 8 has surfaced with support for Lambda expressions. Being a coding enthusiast I just couldn’t hold back my curiosity and got busy with it. After reading official docs and random coding I found it quite easy and interesting enough. It’s one attempt to reduce the verbosity in the source and make code more elegant to the eyes with syntactic sugar. Without further ado let’s get started. Remember all the functional interfaces (interfaces with only one method to be implemented) that were mostly implemented on the fly by the anonymous inner classes like we’ve been doing in swing or sorting? Let’s take an example, here’s the person class with some properties:

public class Person {
    public enum Sex {
        MALE, FEMALE
    String name;
    LocalDate birthday;
    Sex gender;
    String emailAddress;

    public Person(){}

    public Person(String name, LocalDate birthday, Sex gender, String emailAddress) { = name;
        this.birthday = birthday;
        this.gender = gender;
        this.emailAddress = emailAddress;

    public int getAge() {
        return ( - this.birthday.getYear());

    public String toString(){
        return" "+this.getAge();

Now, if we want to sort the list of Persons, we’ll have to implement Comparator interface to provide the sorting criteria. In the former version we would do it using an anonymous inner class like:

personList.sort((new Comparator<Person>() {
            public int compare(Person o1, Person o2) {
                return (o1.getAge() == o2.getAge())?0:((o1.getAge() > o2.getAge())?1:-1);

This can be done in just one line with lesser verbosity using the lambda support:

personList.sort((Person o1,Person o2) ->  (o1.getAge() == o2.getAge())?0:((o1.getAge() > o2.getAge())?1:-1));

or you can do it like:

personList.sort((Person o1,Person o2) ->  {return (o1.getAge() == o2.getAge())?0:((o1.getAge() > o2.getAge())?1:-1);});

{} body is optional with one line statement. The basic idea here is that you can omit the name of the method inside the functional interface and only quote the arguments (like: (Person o1,Person o2)) with the block of code provided using “->”. For more information on the syntax you may refer to the official document.