Posted on by & filed under Content - Highlights and Reviews, java, programming, Programming & Development, Tech.

by Trisha Gee

Trisha Gee is the author of the new video series called “Building Java 8 Web Applications with Microservices” from Addison-Wesley Professional.

If you’re a Java developer you’ll have heard about lambda expressions making their way into Java SE 8. You might not be aware, however, that there’s much more to Java 8 than a new arrow operator. In this article we’re going to look at how lambda expressions and other new features in Java 8 will make your life as a developer easier.

Lambda expressions

The shift in mindset that lambda expressions encourages is the idea of passing behaviour around, not just objects. For example, you can give a method a piece of code to execute:

The Service class contains all the boilerplate needed to instantiate a new web sockets service. It’s a very simple service that connects to a server endpoint (“ws://localhost:8081/tweets/”) that provides data to consume, and starts its own server endpoint (“/users/”, 8083) where it publishes data.  The final parameter is our lambda, which tells our micro-sized service what the business logic is – consume Twitter messages, extract the Twitter username and publish it.

Before lambdas, you might have had copy-and-paste coding as you copy all the service boilerplate code and replace the business logic section for each new service. Or you might have an abstract service class that you override for each individual service. With lambdas, you can be much clearer about separating the infrastructure-level Service code and its business logic.


The Streams API provides a clean way to query your collections, which can decrease the amount of code you have to write. Let’s say, for example, you’ve parsed a message into a List containing all the words in that message. Given you have a Map that maps a word to some mood (happy, sad, angry, playful…) you can find out which moods your message contains with something like:

We turn our word into the lowercase version (here we’re using a Method Reference, which is a shortcut that makes simple lambda expressions even shorter); get the mood associated with that word; filter out any nulls (words that don’t have moods) and finally place them all into a Set. We’re doing a complex set of operations on a collection with no need to write a for-loop or keep track of intermediate state.

If you’re planning to migrate code to Java 8, you could find all the places you currently use a for-loop to see if a series of Stream operations will do the job in fewer, more descriptive lines of code.

New Methods on Collections

The Streams API is a way to execute a pipeline of operations on anything that implements the Collection interface. But it doesn’t stop there – individual collections have also had new helper methods added to them for common operations (thanks to Java 8’s addition of default methods to interfaces).

Let’s consider the case where you want to either get an existing value from a Map, or create a new one and place it in the map.  Before Java 8, you might write:

But now there’s a computeIfAbsent method that does a lot of this for us:

That’s just one example of a helpful new method, take a look at the API documentation, you’ll find a lot more.


Comparators are where we often used Java’s predecessor to lambdas, anonymous inner classes. With the introduction of Streams and the sorted method, we’ll use them more than ever.  The introduction of helper methods on the Comparator interface (made possible by the introduction of static methods on interfaces in Java 8) makes this easier. Previously, to sort alphabetically by Twitter handle, you might have written something like:

Now, you can use Comparator.comparing combined with Method References to declare which value you want to compare:

If you want to sort in descending instead of ascending order, you don’t have to swap the order of the comparison around, you can simply say:

Date & Time

Instead of using java.util.Date, where all the useful-looking methods are deprecated, we have a new Date and Time API. Now we have the idea of a date without needing a time, we have Timezones, Periods and Durations, and we have an API where naming is intuitive and helpful. For example, Instead of having to know that a new Date object represents the time right now, we say:

We can do logical things to the time, like get the time in ten minutes:

and query for specific values:

Java 8 will give you…

In summary, Java 8 has been designed with us, the programmers who use it every day, in mind.  We have lambda expressions to let us pass behaviour around; we have the Streams API to make it easy to manipulate our data; we have new methods on collections that reduce the amount of boilerplate we have to write; we have improvements to Comparators to help us to use Streams features is in an easier and more readable way; and we finally have a Date and Time API which is readable and safe to use. 

Learn More in Safari

For more tips and techniques on how to use all of these features in a real world Java 8 application, watch my new video series “Building Java 8 Applications with Microservices.”

Tags: API, expressions, java, java 8, lambda, microservices,

Comments are closed.