Getting started with lambdas in Java

A good example to get starting with using lambdas in Java is to look at how we might sort a list of items. We want to write a routine that can sort different types of objects on different fields, without having to write a new sort routine for every single combination of object type and field.

Before Java 8, Java provided the Collections.sort() method along with the Comparator interface. This achieves the desired flexibility, since we can implement the relevant method on Comparator to sort on hte field we require. For example, let us suppose that we have a list of Customer objects and wwish to sort them by name. We could then implement our comparator as follows:

static final Comparator CUSTOMER_NAME_COMPARATOR = new Comparator<>() {
	public int compare(Customer c1, Customer c2) {
		return c1.getName().compareTo(c2.getName());
	}
}

(To emphasise: Comparator is an interface, and in this case we are creating an anonymous instance of that interface. We use new with the name of an interface and the Java compiler will create a placeholder class for us that implements the interface with our method.) Then, on a List of Customers, we can call:

Collections.sort(customers, CUSTOMER_NAME_COMPARATOR);

Declaring the comparator inline, we could make this code slightly more succinct:

Collections.sort(customers, new Comparator<>() {
	public int compare(Customer c1, Customer c2) {
		return c1.getName().compareTo(c2.getName());
	}
});

However, the fact remains that this code contains a lot of "boilerplate" around the single line that actually performs the name comparison.

Lambdas to the rescue

Using a lamba instead, we can effectively declare our comparator to be the single line of code that actually performs the comparison. In other words, we can simply write:

Collections.sort(customers, (c1, c2) -> c1.getName().compareTo(c2.getName()));

Here, (c1, c2) -> c1.getName().compareTo(c2.getName()) is a lambda expression. Like a method, it starts with a parameter list in brackets. However, we no longer need to specify parameter types: these will be determined from the matching interface (Comparator). After the special "arrow" syntax , we then simply specify the line of code to execute, which may refer to the local parameters c1 and c2.

For this conversion from interface implementation to lambda to be possible, the Java language has been extended with the necessary syntax and logic. The previous line of code is already a huge improvement on the code it replaces in terms of its succinctness. However, Java 8 also includes API updates to make even more powerful use of lambdas. Using the Comparators utility class, we can go a stage further and actually write:

Collections.sort(customers, Comparators.comparingBy(c -> c.getName()));

or, using a further syntax innovation introduced with lambdas:

Collections.sort(customers, Comparators.comparingBy(Customer::getName));

Next: learning more about lambdas

This brief overview combined with some experimentation will get you started with lambdas. However, to take full advantages of lambdas, there are a few more details that you will need to know about, notably:


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.