For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email:
2019 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Development editor: Kevin HarreldTechnical development editor: Dennis SellingerReview editor: Aleksandar DragosavljeviProject manager: Deirdre HiamCopy editors: Heidi Ward and Kathy SimpsonProofreader: Carol ShieldsTechnical proofreader: Jean-Franois MorinTypesetter: Dennis DalinnikCover designer: Marija Tudor
Praise for the previous edition, Java 8 in Action, by Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft.
A great and concise guide to whats new in Java 8, with plenty of examples to get you going in a hurry.
Jason Lee, Oracle
The best guide to Java 8 that will ever be written!
William Wheeler, ProData Computer Systems
The new Streams API and lambda examples are especially useful.
Steve Rogers, CGTek, Inc.
A must-have to get functional with Java 8.
Mayur S. Patil, MIT Academy of Engineering
Helpful as a concise, practice-oriented guide to the exciting new features of Java 8. Functional interfaces and spliterators, oh my!
Will Hayworth, Developer, Atlassian
Preface
Back in 1998 when I was eight years old, I picked up my first book on computingon JavaScript and HTML. Little did I know that opening that book would transform my life by exposing me to programming languages and the amazing things I could do with them. I was hooked. Every so often, I still find a new programming language feature that revives this excitement because it enables me to write clearer, more concise code in half the time. I hope that the new ideas in Java 8, Java 9, and Java 10, incorporated from functional programming and explored in this book, inspire you in the same way.
So, you may wonder, how did this bookand its second editioncome about?
Well, back in 2011, Brian Goetz (the Java Language Architect at Oracle) was sharing various proposals to add lambda expressions to Java, with the aim of getting the community involved. These rekindled my excitement, and I started to evangelize the ideas, organizing Java 8 workshops at various developer conferences and giving lectures to students at the University of Cambridge.
By April 2013, word had spread, and our publisher at Manning emailed asking whether I was interested in writing a book about lambdas in Java 8. At the time I was a humble second-year PhD candidate, and that seemed to be a bad idea because it would interfere with my thesis submission schedule. On the other hand, carpe diem. I thought writing a short book shouldnt be too much work, right? (It was only later that I realized I was utterly wrong!) So, I sought advice from my PhD supervisor, Professor Alan Mycroft, who, it turned out, was ready to support me in this adventure (even offering to help in such non-PhD workIm forever in his debt). A few days later, we met fellow Java 8 evangelist Mario Fusco, who had vast professional experience and had become well known at major developer conferences for his talks on functional programming.
We quickly realized that by combining our energy and diverse backgrounds we could deliver, not just a short book on Java 8 lambdas, but instead a book that, we hope, the Java community will still be reading in five or ten years. We had a unique opportunity to discuss many topics in depth that will benefit Java programmers and open doors to a new universe of ideas: functional programming.
Now, its 2018, and we find that the first edition amazingly sold 20,000 copies, Java 9 has just been released, Java 10 is about to be released, and time has dulled the memory of many long nights of editing. So, here it isthe second edition Modern Java in Action, covering Java 8, Java 9, and Java 10. We hope you will enjoy it!
R AOUL -G ABRIEL U RMA C AMBRIDGE SPARK
Acknowledgments
This book would not have been possible without the support of many amazing people:
- Personal friends and people who provided valuable reviews and suggestions on a volunteer basis: Richard Walker, Jan Saganowski, Brian Goetz, Stuart Marks, Cem Redif, Paul Sandoz, Stephen Colebourne, igo Mediavilla, Allahbaksh Asadullah, Tomasz Nurkiewicz, and Michael Mller
- Our Manning Early Access Program (MEAP) readers who posted comments in the Author Online forum
- The reviewers from the development process who provided helpful feedback: Antonio Magnaghi, Brent Stains, Franziska Meyer, Furkan Kamachi, Jason Lee, Jrn Dinkla, Lochana Menikarachchi, Mayur Patil, Nikolaos Kaintantzis, Simone Bordet, Steve Rogers, Will Hayworth, and William Wheeler
- Kevin Harreld, our development editor at Manning, who was very patient in answering all our questions and concerns, provided detailed feedback for each of our draft chapters, and supported us in all possible ways
- Dennis Selinger and Jean-Franois Morin, who provided a thorough technical proofread of the manuscript shortly before it went to production; and Al Scherer, who provided technical expertise during development
Raoul-Gabriel Urma
First and foremost, Id like to thank my parents for their endless love and support in my life. This little dream of writing a book has now come true! Next, I would like to express my eternal gratitude to Alan Mycroft, my PhD supervisor and coauthor, for his trust and support. Id also like to thank my coauthor Mario Fusco for sharing this fun journey. Finally, Id like to thank friends who have provided mentorship, useful advice, and encouragement in my life: Sophia Drossopoulou, Aidan Roche, Alex Buckley, Haadi Jabado, and Jaspar Robertson. You guys rock!
Mario Fusco
Id like to especially thank my wife, Marilena, whose boundless patience allowed me to stay focused on the book, and our daughter, Sofia, because the infinite chaos she can produce allowed me to get creatively distracted from the book. As youll discover reading the book, Sofia also taught us, like only a two-year-old baby girl can, the difference between internal and external iteration. Id like to also thank Raoul-Gabriel Urma and Alan Mycroft, with whom I shared the (big) joys and the (small) pains of this writing experience.