Introducing Java 8
Introducing Java 8
Java 8
A Quick-Start Guide to Lambdas
and Streams
Raoul-Gabriel Urma
Additional
Resources
4 Easy Ways to Learn More and Stay Current
Programming Newsletter
Get programming r elated news and content delivered weekly to your inbox.
oreilly.com/programming/newsletter
OReilly Radar
Read more insight and analysis about emerging technologies.
radar.oreilly.com
Conferences
Immerse yourself in learning at an upcoming OReilly conference.
conferences.oreilly.com
2015 OReilly Media, Inc. The OReilly logo is a registered trademark of OReilly Media, Inc. #15305
Introducing Java 8
Raoul-Gabriel Urma
Introducing Java 8
by Raoul-Gabriel Urma
Copyright 2015 OReilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA
95472.
OReilly books may be purchased for educational, business, or sales promotional use.
Online editions are also available for most titles (http://safaribooksonline.com). For
more information, contact our corporate/institutional sales department:
800-998-9938 or [email protected].
First Edition
978-1-491-93434-0
[LSI]
Table of Contents
1
3
3
11
13
13
14
15
16
18
18
3. Adopting Streams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
The Need for Streams
What Is a Stream?
Stream Operations
Filtering
Matching
Finding
Mapping
Reducing
Collectors
Putting It All Together
Parallel Streams
Summary
19
20
21
21
22
22
23
23
24
24
26
27
CHAPTER 1
Java has changed! The new version of Java, released in March 2014,
called Java 8, introduced features that will change how you program
on a day-to-day basis. But dont worrythis brief guide will walk
you through the essentials so you can get started.
This first chapter gives an overview of Java 8s main additions. The
next two chapters focus on Java 8s main features: lambda expressions
and streams.
There were two motivations that drove the changes in Java 8:
Better code readability
Simpler support for multicore
Code Readability
Java can be quite verbose, which results in reduced readability.
In other words, it requires a lot of code to express a simple concept.
Heres an example: say you need to sort a list of invoices in decreas
ing order by amount. Prior to Java 8, youd write code that looks
like this:
Collections.sort(invoices, new Comparator<Invoice>() {
public int compare(Invoice inv1, Invoice inv2) {
return Double.compare(inv2.getAmount(), inv1.getAmount());
}
});
In this kind of coding, you need to worry about a lot of small details
in how to do the sorting. In other words, its difficult to express a
simple solution to the problem statement. You need to create a
Comparator object to define how to compare two invoices. To do
that, you need to provide an implementation for the compare
method. To read this code, you have to spend more time figuring
out the implementation details instead of focusing on the actual
problem statement.
In Java 8, you can refactor this code as follows:
invoices.sort(comparingDouble(Invoice::getAmount).reversed());
Dont worry about the details of this code for now; youll see
the Streams API in depth in Chapter 3. For now, think of a Stream
as a new abstraction for expressing data processing queries in a
readable way.
2
Multicore
The second big change in Java 8 was necessitated by multicore pro
cessors. In the past, your computer would have only one processing
unit. To run an application faster usually meant increasing the per
formance of the processing unit. Unfortunately, the clock speeds of
processing units are no longer getting any faster. Today, the vast
majority of computers and mobile devices have multiple processing
units (called cores) working in parallel.
Applications should utilize the different processing units for
enhanced performance. Java applications typically achieve this by
using threads. Unfortunately, working with threads tends to be diffi
cult and error-prone and is often reserved for experts.
The Streams API in Java 8 lets you simply run a data processing
query in parallel. For example, to run the preceding code in parallel
you just need to use parallelStream() instead of stream():
List<Integer> ids = invoices.parallelStream()
.filter(inv -> inv.getAmount() > 1_000)
.map(Invoice::getId)
.collect(Collectors.toList());
In Chapter 3, I will discuss the details and best practices when using
parallel streams.
Lambda Expressions
Lambda expressions let you pass around a piece of code in a concise
way. For example, say you need to get a Thread to perform a task.
You could do so by creating a Runnable object, which you then pass
as an argument to the Thread:
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("Hi");
}
Multicore
};
new Thread(runnable).start();
Using lambda expressions, on the other hand, you can rewrite the
previous code in a much more readable way:
new Thread(() -> System.out.println("Hi")).start();
Method References
Method references make up a new feature that goes hand in hand
with lambda expressions. They let you select an existing method
defined in a class and pass it around. For example, say you need to
compare a list of strings by ignoring case. Currently, you would
write code that looks like this:
List<String> strs = Arrays.asList("C", "a", "A", "b");
Collections.sort(strs, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
The code just shown is extremely verbose. After all, all you need is
the method compareToIgnoreCase. Using method references, you
can explicitly say that the comparison should be performed using
the method compareToIgnoreCase defined in the String class:
Collections.sort(strs, String::compareToIgnoreCase);
Streams
Nearly every Java application creates and processes collections.
Theyre fundamental to many programming tasks since they let you
group and process data. However, working with collections can be
quite verbose and difficult to parallelize. The following code illus
trates how verbose processing collections can be. It processes a list
of invoices to find the IDs of training-related invoices sorted by the
invoices amount:
4
Java 8 introduces a new abstraction called Stream that lets you pro
cess data in a declarative way. In Java 8, you can refactor the preced
ing code using streams, like so:
List<Integer> invoiceIds =
invoices.stream()
.filter(inv -> inv.getTitle().contains("Training"))
.sorted(comparingDouble(Invoice::getAmount)
.reversed())
.map(Invoice::getId)
.collect(Collectors.toList());
Enhanced Interfaces
Interfaces in Java 8 can now declare methods with implementation
code thanks to two improvements. First, Java 8 introduces default
methods, which let you declare methods with implementation code
inside an interface. They were introduced as a mechanism to evolve
the Java API in a backward-compatible way. For example, youll see
that in Java 8 the List interface now supports a sort method that is
defined as follows:
default void sort(Comparator<? super E> c) {
Collections.sort(this, c);
}
Immutability
One of the problems with Date and Calendar is that they
werent thread-safe. In addition, developers using dates as part
of their API can accidentally update values unexpectedly. To
prevent these potential bugs, the classes in the new Date and
6
Time API are all immutable. In other words, you cant change
an objects state in the new Date and Time API. Instead, you use
a method to return a new object with an updated value.
The following code exemplifies various methods available in the
new Date and Time API:
ZoneId london = ZoneId.of("Europe/London");
LocalDate july4 = LocalDate.of(2014, Month.JULY, 4);
LocalTime early = LocalTime.parse("08:45");
ZonedDateTime flightDeparture = ZonedDateTime.of(july4, early,
london);
System.out.println(flightDeparture);
LocalTime from = LocalTime.from(flightDeparture);
System.out.println(from);
ZonedDateTime touchDown
= ZonedDateTime.of(july4,
LocalTime.of (11, 35),
ZoneId.of("Europe/Stockholm"));
Duration flightLength = Duration.between(flightDeparture, touch
Down);
System.out.println(flightLength);
// How long have I been in continental Europe?
ZonedDateTime now = ZonedDateTime.now();
Duration timeHere = Duration.between(touchDown, now);
System.out.println(timeHere);
CompletableFuture
Java 8 introduces a new way to think about asynchronous program
ming with a new class, CompletableFuture. Its an improvement on
the old Future class, with operations inspired by similar design
choices made in the new Streams API (i.e., declarative flavor and
ability to chain methods fluently). In other words, you can declara
tively process and compose multiple asynchronous tasks.
Optional
Java 8 introduces a new class called Optional. Inspired by functional
programming languages, it was introduced to allow better modeling
in your codebase when a value may be present or absent. Think of it
as a single-value container, in that it either contains a value or is
empty. Optional has been available in alternative collections frame
works (like Guava), but is now available as part of the Java API. The
other benefit of Optional is that it can protect you against
NullPointerExceptions. In fact, Optional defines methods to force
you to explicitly check the absence or presence of a value. Take the
following code as an example:
getEventWithId(10).getLocation().getCity();
if(event != null) {
Location location = event.getLocation();
if(location != null) {
return location.getCity();
}
}
return "TBC";
}
CHAPTER 2
11
result.add(inv);
}
}
return result;
}
With this useful code, you can cope with any requirement changes
involving any property of an Invoice object. You just need to create
different InvoicePredicate objects and pass them to the
findInvoices method. In other words, you have parameterized the
behavior of findInvoices. Unfortunately, using this new method
introduces additional verbosity, as shown here:
List<Invoice> expensiveInvoicesFromOracle
= findInvoices(invoices, new InvoicePredicate() {
public test(Invoice inv) {
return inv.getAmount() > 10_000
&& inv.getCustomer() == Customer.ORACLE;
}
});
In other words, you have more flexibility but less readability. Ideally,
you want both flexibility and conciseness, and thats where lambda
12
expressions come in. Using this feature, you can refactor the preced
ing code as follows:
List<Invoice> expensiveInvoicesFromOracle
= findInvoices(invoices, inv ->
inv.getAmount() > 10_000
&& inv.getCustomer() ==
Customer.ORACLE);
13
You use the second form when the body of the lambda expression
contains one or multiple statements. Note that you have to use curly
braces surrounding the body of the lambda expression:
(parameters) -> { statements;}
Generally, one can omit the type declarations from the lambda
parameters if they can be inferred. In addition, one can omit the
parentheses if there is a single parameter.
The important point here is that lambda expressions let you create
an instance of a functional interface. The body of the lambda
expression provides the implementation for the single abstract
method of the functional interface. As a result, the following uses of
Runnable via anonymous classes and lambda expressions will pro
duce the same output:
14
Method References
Method references let you reuse existing method definitions and
pass them around just like lambda expressions. They are useful in
certain cases to write code that can feel more natural and readable
compared to lambda expressions. For example, you can find hidden
files using a lambda expression as follows:
File[] hiddenFiles
den());
mainDirectory.listFiles(f
->
f.isHid
Using a method reference, you can directly refer to the method isH
idden using the double colon syntax (::).
File[] hiddenFiles = mainDirectory.listFiles(File::isHidden);
Method References
15
A constructor reference:
Supplier<List<String>> listOfString = List::new;
Now youll see exactly how to use the Java 8 features youve learned
so far to refactor this code so its more readable and concise.
First, notice that Comparator is a functional interface because it only
declares a single abstract method called compare, which takes two
objects of the same type and returns an integer. This is an ideal sit
uation for a lambda expression, like this one:
Collections.sort(invoices,
(Invoice inv1, Invoice inv2) -> {
return Double.compare(inv2.getAmount(),
inv1.getAmount());
});
16
In Java 8, the List interface supports the sort method, so you can
use that instead of Collections.sort:
invoices.sort((Invoice inv1, Invoice inv2)
-> Double.compare(inv2.getAmount(),
inv1.getAmount()));
Finally, lets tidy up the code and use an import static and also get
rid of the local variable holding the Comparator object to produce a
solution that reads like the problem statement:
import static java.util.Comparator.comparingDouble;
invoices.sort(comparingDouble(Invoice::getAmount));
17
Summary
Here are the key concepts from this chapter:
A lambda expression can be understood as a kind of anony
mous function.
Lambda expressions and the behavior parameterization pattern
let you write code that is both flexible and concise.
A functional interface is an interface that declares a single
abstract method.
Lambda expressions can only be used in the context of a func
tional interface.
Method references can be a more natural alternative to lambda
expressions when you need to reuse an existing method and
pass it around.
In the context of testing, extract large lambda expressions
into separate methods that you can then inject using method
references.
18
CHAPTER 3
Adopting Streams
In this chapter, youll learn how to adopt the Streams API. First,
youll gain an understanding behind the motivation for the Streams
API, and then youll learn exactly what a stream is and what its used
for. Next, youll learn about various operations and data processing
patterns using the Streams API, and about Collectors, which let you
write more sophisticated queries. Youll then look at a practical
refactoring example. Finally, youll learn about parallel streams.
19
Youll see how this code works in more detail later in this chapter.
What Is a Stream?
So what is a stream? Informally, you can think of it as a fancy itera
tor that supports database-like operations. Technically, its a
sequence of elements from a source that supports aggregate opera
tions. Heres a breakdown of the more formal definition:
Sequence of elements
A stream provides an interface to a sequenced set of values of a
specific element type. However, streams dont actually store ele
ments; theyre computed on demand.
Source
Streams consume from a data-providing source such as collec
tions, arrays, or I/O resources.
Aggregate operations
Streams support database-like operations and common opera
tions from functional programming languages, such as filter,
map, reduce, findFirst, allMatch, sorted, and so on.
20
Adopting Streams
Stream Operations
The Stream interface in java.util.stream.Stream defines many
operations, which can be grouped into two categories:
Operations such as filter, sorted, and map, which can be con
nected together to form a pipeline
Operations such as collect, findFirst, and allMatch, which
terminate the pipeline and return a result
Stream operations that can be connected are called intermediate
operations. They can be connected together because their return
type is a Stream. Intermediate operations are lazy and can often be
optimized. Operations that terminate a stream pipeline are called
terminal operations. They produce a result from a pipeline such as a
List, Integer, or even void (i.e., any nonstream type).
Lets take a tour of some of the operations available on streams.
Refer to the java.util.stream.Stream interface for the complete
list.
Filtering
There are several operations that can be used to filter elements from
a stream:
filter
21
distinct
Matching
A common data processing pattern is determining whether some
elements match a given property. You can use the anyMatch,
allMatch, and noneMatch operations to help you do this. They all
take a predicate as an argument and return a boolean as the result.
For example, you can use allMatch to check that all elements in a
stream of invoices have a value higher than 1,000:
boolean expensive =
invoices.stream()
.allMatch(inv -> inv.getAmount() > 1_000);
Finding
In addition, the Stream interface provides the operations findFirst
and findAny for retrieving arbitrary elements from a stream. They
can be used in conjunction with other stream operations such as
filter. Both findFirst and findAny return an Optional object
(which we discussed in Chapter 1):
Optional<Invoice> =
invoices.stream()
.filter(inv ->
inv.getCustomer() == Customer.ORACLE)
.findAny();
22
Adopting Streams
Mapping
Streams support the method map, which takes a Function object as
an argument to turn the elements of a stream into another type.
The function is applied to each element, mapping it into a new ele
ment.
For example, you might want to use it to extract information from
each element of a stream. This code returns a list of the IDs from a
list of invoices:
List<Integer> ids
= invoices.stream()
.map(Invoice::getId)
.collect(Collectors.toList());
Reducing
Another common pattern is that of combining elements from a
source to provide a single value. For example, calculate the invoice
with the highest amount or calculate the sum of all invoices
amounts. This is possible using the reduce operation on streams,
which repeatedly applies an operation to each element until a result
is produced.
As an example of a reduce pattern, it helps to first look at how you
could calculate the sum of a list using a for loop:
int sum = 0;
for (int x : numbers) {
sum += x;
}
Mapping
23
Collectors
The operations you have seen so far were either returning another
stream (i.e., intermediate operations) or returning a value, such as a
boolean, an int, or an Optional object (i.e., terminal operations).
By contrast, the collect method is a terminal operation. It lets you
accumulate the elements of a stream into a summary result.
The argument passed to collect is an object of type
java.util.stream.Collector. A Collector object essentially
describes a recipe for accumulating the elements of a stream into a
final result. The factory method Collectors.toList() used earlier
returns a Collector object describing how to accumulate a stream
into a List. However, there are many similar built-in collectors
available, which you can see in the class Collectors. For example,
you can group invoices by customers using Collectors.groupingBy
as shown here:
Map<Customer, List<Invoice>> customerToInvoices
=
invoices.stream().collect(Collectors.group
ingBy(Invoice::getCustomer));
24
Adopting Streams
Now youll refactor this code step-by-step using the Streams API.
First, you may notice that you are using an intermediate container to
store invoices that have the customer Customer.ORACLE and
"Training" in the title. This is the use case for using the filter
operation:
Stream<Invoice> oracleAndTrainingInvoices
= invoices.stream()
.filter(inv ->
inv.getCustomer() == Customer.ORACLE)
.filter(inv ->
inv.getTitle().contains("Training"));
Next, you need to sort the invoices by their amount. You can use the
new utility method Comparator.comparing together with the
method sorted, as shown in the previous chapter:
Stream<Invoice> sortedInvoices
=
oracleAndTrainingInvoices.sorted(comparingDou
ble(Invoice::getAmount));
25
Next, you need to extract the IDs. This is a pattern for the map oper
ation:
Stream<Integer> ids
= sortedInvoices.map(Invoice::getId);
Finally, youre only interested in the first five invoices. You can use
the operation limit to stop after those five. Once you tidy up the
code and use the collect operation, the final code is as follows:
List<Integer> firstFiveIds
= invoices.stream()
.filter(inv ->
inv.getCustomer() == Customer.ORACLE)
.filter(inv ->
inv.getTitle().contains("Training"))
.sorted(comparingDouble(Invoice::getAmount))
.map(Invoice::getId)
.limit(5)
.collect(Collectors.toList());
You can observe that in the old-style Java code, each local variable
was stored once and used once by the next stage. Using the Streams
API, these throwaway local variables are eliminated.
Parallel Streams
The Streams API supports easy data parallelism. In other words, you
can explicitly ask for a stream pipeline to be performed in parallel
without thinking about low-level implementation details. Behind
the scenes, the Streams API will use the Fork/Join framework, which
will leverage the multiple cores of your machine.
All you need to do is exchange stream() with parallelStream().
For example, heres how to filter expensive invoices in parallel:
List<Invoice> expensiveInvoices
= invoices.parallelStream()
.filter(inv -> inv.getAmount() > 10_000)
.collect(Collectors.toList());
Stream<Invoice> expensiveInvoices
= invoices.stream()
.filter(inv -> inv.getAmount() > 10_000);
List<Invoice> result
26
Adopting Streams
= expensiveInvoices.parallel()
.collect(Collectors.toList());
Number of cores
Typically, the more cores available, the more parallelism you
can get.
In practice, I advise that you benchmark and profile your code if
you want a performance improvement. Java Microbenchmark
Harness (JMH) is a popular framework maintained by Oracle that
can help you with that. Without care, you could get poorer perfor
mance by simply switching to parallel streams.
Summary
Here are the most important takeaways from this chapter:
A stream is a sequence of elements from a source that supports
aggregate operations.
Summary
27
28
Adopting Streams
Acknowledgments
I would like to thank my parents for their continuous support. In
addition, I would like to thank Alan Mycroft and Mario Fusco, with
whom I wrote the book Java 8 in Action. Finally, I would also like to
thank Richard Warburton, Stuart Marks, Trisha Gee, and the
OReilly staff, who provided valuable reviews and suggestions.