About this Book - Java 8 in Action: Lambdas, streams, and functional-style programming (2015)

Java 8 in Action: Lambdas, streams, and functional-style programming (2015)

About this Book

Put simply, the new features in Java 8 are the biggest change to Java in the 18 years since Java 1.0 was released. Nothing has been taken away, so all your existing Java code continues to work—but the new features provide powerful new idioms and design patterns to help you write clearer, more concise code. At first you might think (as with all new features), “Why are they changing my language again?” But then, after a bit of practice, comes the revelation that you’ve just used the features to write shorter, clearer code in half the time you expected—and you realize you could never go back to “old Java” again.

This book, Java 8 in Action: Lambdas, streams, and functional-style programming, is written to get you over that initial hump of “sounds good in principle, but it’s all a bit new and unfamiliar” and into coding like a native.

“Perhaps,” you might think, “but lambdas, functional programming—aren’t those the sort of things that bearded sandal-wearing academics talk about in their ivory towers?” They might, but Java 8 has incorporated just the right balance of ideas into Java to gain many of their advantages in a way that’s comprehensible to ordinary Java programmers. And this book tells the story from the ordinary-programmer viewpoint, with an occasional “how this arose” for perspective.

“Lambdas—that sounds Greek to me!” Yes, it might, but it’s a great idea for enabling you to write concise Java programs. Many of you are familiar with event handlers and callbacks, where you register an object containing a method to be used when some event happens. Lambdas make this sort of idea much more widely usable in Java. Put simply, lambdas, and their friends, method references, provide the ability to concisely pass code or methods as arguments to be executed in the middle of doing something else. You’ll see in this book how this idea occurs more frequently than you might think: from simply parameterizing a sort method with code to do the comparison to expressing complex queries on collections of data using the new Streams API.

“Streams—what are they?” They’re a great new Java 8 addition. They behave like collections but have several advantages that enable new styles of programming. First, if you’ve ever programmed using a database query language such as SQL, you’ll recognize that it enables queries to be written in a few lines that would take many lines in Java. Java 8 streams support this concise database-queries style of programming—but with Java syntax and none of the need to know about databases! Second, streams are designed so that not all their data needs to be in memory (or even computed) at once. Thus you can process streams that are too big to fit in your computer memory. But Java 8 can optimize operations on streams in a way that Java can’t do for collections—for example, it can group together several operations on the same stream, so that the data is traversed only once instead of expensively traversing it multiple times. Even better, Java can automatically parallelize stream operations for you (unlike collections).

“And functional-style programming, what’s that?” It’s another style of programming, just like object-oriented programming, but centered on using functions as values, just as we mentioned previously when discussing lambdas.

What’s great about Java 8 is that it incorporates many of the best ideas from functional programming into the familiar Java syntax. The fine design choices enable you to see functional-style programming in Java 8 as an additional set of design patterns and idioms to enable you to write clearer, more concise code in less time. Think of it as having a wider range of weapons in your programming armory.

Oh yes, in addition to these features that lean on big conceptual additions to Java, we also explain the many other useful Java 8 features and updates such as default methods, the new Optional class, CompletableFuture, and the new Date and Time API.

But hey, this is an overview, and it’s time now for us to leave you to read the book.

Roadmap

Java 8 in Action is divided into four parts: “Fundamentals,” “Functional-style data processing,” “Effective Java 8 programming,” and “Beyond Java 8.” We strongly recommend that you read the chapters in order because many of the concepts presented build on previous chapters. Most chapters include several quizzes to help you work through the material.

The first part of the book contains three chapters that aim to get you started with Java 8. By the end of this first part you’ll have a full understanding of what lambda expressions are, and you’ll be able to write code that’s both concise and flexible enough to easily adapt to changing requirements:

· In chapter 1, we summarize the main changes to Java (lambda expressions, method references, streams, and default methods) and set the scene for the book.

· In chapter 2, you’ll learn about behavior parameterization, a software development pattern that Java 8 heavily relies on and is the motivation for lambda expressions.

· Chapter 3 gives a full explanation, with code examples and quizzes at every step, of the concepts of lambda expressions and method references.

The second part takes a close look at the new Streams API. By the end of it you’ll have a full understanding of what streams are and how you can use them in your Java applications to process a collection of data concisely and efficiently:

· Chapter 4 introduces the concept of streams and explains how they compare with collections.

· Chapter 5 investigates in detail the stream operations available to express sophisticated data processing queries. We’ll look at many patterns such as filtering, slicing, finding, matching, mapping, and reducing.

· Chapter 6 covers collectors—a feature of the Streams API that lets you express even more complex data processing queries.

· In chapter 7, you’ll learn about how streams can automatically run in parallel and leverage your multicore architectures. In addition, you’ll learn about various pitfalls to avoid in order to use parallel streams correctly and effectively.

The third part of this book explores various Java 8 topics that will make you more effective at using Java 8 and enhance your codebase with modern idioms:

· Chapter 8 explores how you can improve your existing code using new Java 8 features and a few recipes. In addition, it explores vital software development techniques such as design patterns, refactoring, testing, and debugging.

· In chapter 9, you’ll learn what default methods are, how you can evolve APIs in a compatible way using them, some practical usage patterns, and rules for using default methods effectively.

· Chapter 10 covers the new java.util.Optional class, which allows you to both design better APIs and reduce null pointer exceptions.

· Chapter 11 explores CompletableFuture, which lets you express complex asynchronous computations in a declarative way—paralleling the design of the Streams API.

· Chapter 12 explores the new Date and Time API, which greatly improves on the previous error-prone APIs for working with dates and time.

In the final part of this book, we draw back a little with a tutorial introduction to writing effective functional-style programs in Java, and we offer a comparison of Java 8 features with those of Scala:

· Chapter 13 gives a full tutorial to functional programming, introduces some of its terminology, and explains how to write functional-style programs in Java 8.

· Chapter 14 covers more-advanced functional programming techniques including higher-order functions, currying, persistent data structures, lazy lists, and pattern matching. You can view this chapter as a mix of practical techniques to apply in your codebase as well as academic information that will make you a more knowledgeable programmer.

· Chapter 15 follows by discussing how Java 8 features compare to features in the Scala language—a language that, like Java, is implemented on top of the JVM and that has evolved quickly to threaten some aspects of Java’s niche in the programming language ecosystem.

· In chapter 16, we review the journey of learning about Java 8 and the gentle push toward functional-style programming. In addition, we speculate on what future enhancements and great new features may be in Java’s pipeline beyond Java 8.

Finally, there are four appendixes, which cover a number of other topics related to Java 8. Appendix A summarizes other minor Java 8 language features that we didn’t discuss in the book. Appendix B gives an overview of other main additions to the Java library that you may find useful.Appendix C is a continuation of part 2 and looks at advanced uses of streams. Appendix D explores how the Java compiler implements lambda expressions behind the scenes.