Java 8 Example – Lambdas


A few months ago I wrote about how our core Java courses are being updated to Java 8.  Now that the updates are a little further along we also have a course map that can show which Java 8 courses we have might be appropriate for you.  You can find that here:

Java Course Map

I thought that it might be good to offer a quick example of some of the things in Java 8 on our blog.  Since you can’t really talk about Java 8 without discussing Lambda expressions I figured I would start there.

Lambda expressions are probably the biggest addition to the Java language with Java SE 8.  A lambda expressions creates a function as a first-class object.  Lambdas allow functions to be passed as parameters or return types and stored as data.  Although this is not the only use, lambdas are a great fit for anonymous inner classes that implement “functional interfaces” with only one method.

As an example, lets look at how lambdas could change how we might sort a list of objects using the Collections.sort method.  Collections.sort takes the list to be sorted and a ‘Comparator’ implementation to compare elements in the list for sorting.  The Comparator interface is a “functional interface” since it declares only a single method.  This is one of the most common uses of anonymous inner classes, or AIC, where we need an implementation of a single method but don’t want to have a completely separate class definition.  We just need something temporary for that situation.

interface Comparator<T> {

     public int compare(T arg0, T arg1);

}

Prior to Java 8 you could create an anonymous inner class to define the implementation.  Notice how what is in bold is the core of the logic we need to determine how things are compared.  The rest of the syntax though is just required to declare the AIC.  Note that all you need to know are the parameters and what is done with those parameters to return the answer.

List<Person> list = …; // initialized

Collections.sort(list, new Comparator<Person>() {

     public int compare(Person p1, Person p2) {

          return p1.getName().compareTo(p2.getName());

     }

});

With a Java 8 lambda expression the syntax about declaring the AIC is gone and we are just left with the core function of comparing the two Person objects.  The ‘->’ is key to declaring the lambda expression.  You can even omit the types of parameters since these can be inferred from the fact that the list being sorted contains Person instances.

List<Person> list = …; // initialized

Collections.sort(list, (p1, p2) –>

     return p1.getName().compareTo(p2.getName()));

Notice how much simpler the code is to read without the declaration of the AIC.  All that is left is the core logic of how the two parameters are turned into the return value of being compared.  Although not shown here, you could also declare a variable using one of the new types in the ‘java.util.function’ package so that the Lambda expression can be reused, still without creating a full class definition.

Now what if you need several statements to implement the function that is the Lambda expression?  Although Lambda expressions can include curly brackets and several statements, it is also possible to use a “Method Reference”.  This allows you to reference a method rather than complicating the syntax of the Lambda expression with extra curly brackets, one thing we were trying to avoid with the AIC declaration.  The name of the method you reference can be anything, as long as it has the appropriate parameter types and return type for what you are using it for.  The method referenced does not even need to be in the same class although that is what is shown in the example below.

Collections.sort(list, this::myCompare);

// elsewhere in the class

private int myCompare(Person p1, Person p2) {

     int result = p1.getName().compareTo(p2.getName());

     result += p1.getCity().compareTo(p2.getCity());

     return result;

}

This is not the only instance of Lambda usage.  There are many other places, one of note being the Collection Stream API, where the addition of Lambda expressions can simplify and/or expand the type of code and functionality available in Java 8.  If you want more complete information about the changes in Java 8 and how they might be used to improve the Java code of your Java applications, be sure to take our course WA2493 What’s New In Java 8

  1. No comments yet.
(will not be published)

*