To my boys: Joe, Isaac, Daniel, and Ben
About the Author
Maurice Naftalin has over 30 years experience in IT as a developer, designer, architect, manager, teacher, and author. Naftalin, a certified Java programmer, has worked in every release of Java to date. His experience in Java and business gives him a unique perspective on the fundamental change that comes with introducing lambda expressions in Java SE 8. Naftalin is a frequent presenter at conferences worldwide, including the annual JavaOne. He runs a popular tutorial site in collaboration with Oracles development team, www.lambdafaq.org, focused on the new language features in Java 8.
About the Technical Editors
Stuart Marks works on the JDK Core Libraries team in the Java Platform Group at Oracle. He is currently working on lambda, streams, and collections, as well as improving test quality and performance. He has previously worked on JavaFX and Java ME at Sun Microsystems. He has over 20 years of software platform product development experience in the areas of window systems, interactive graphics, and mobile and embedded systems. Stuart holds a masters degree in Computer Science and a bachelors degree in Electrical Engineering from Stanford University. He lives with his wife and daughter in California.
Brian Goetz is one of the leading authorities on Java programming. He is the author of the very successful Java Concurrency in Practice, as well as over 75 articles on Java development. He was the specification lead for JSR-335 (Lambda Expressions for the Java Language) and has served on numerous other JCP Expert Groups. Brian is the Java Language Architect at Oracle.
Copyright 2015 by Maurice Naftalin. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system, without the prior written permission of the publisher.
ISBN: 978-0-07-182963-2
MHID: 0-07-182963-6
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07182962-5, MHID: 0-07-182962-8.
eBook conversion by codeMantra
Version 1.0
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill Education eBooks are available at special quantity discounts to use as premiums and sales promotions or for use in corporate training programs. To contact a representative, please visit the Contact Us page at www.mhprofessional.com.
Oracle and Java are registered trademarks of Oracle Corporation and/or its affiliates. All other trademarks are the property of their respective owners, and McGraw-Hill Education makes no claim of ownership by the mention of products that contain these marks.
Screen displays of copyrighted Oracle software programs have been reproduced herein with the permission of Oracle Corporation and/or its affiliates.
Information has been obtained by Publisher from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Publisher, or others, Publisher does not guarantee to the accuracy, adequacy, or completeness of any information included in this work and is not responsible for any errors or omissions or the results obtained from the use of such information.
Oracle Corporation does not make any representations or warranties as to the accuracy, adequacy, or completeness of any information contained in this Work, and is not responsible for any errors or omissions.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill Educations prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms.
THE WORK IS PROVIDED AS IS. MCGRAW-HILL EDUCATION AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill Education and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill Education nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill Education has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill Education and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
Contents
Foreword
J ava SE 8 may well represent the largest change ever to the Java language and libraries. Since youre reading a book called Mastering Lambdas, you probably already know that the biggest new feature is the addition of lambda expressions. Depending on your perspective, this evolution began in 2009 (when Project Lambda was launched), or 2006 (when several proposals were made for adding closures to Java), or 1997 (when inner classes were added), or 1941 (when Alonzo Church published his foundational work on the theory of computing, from which lambda expressions get their name).
However long it took to get here, its about time! While it may initially appear that lambda expressions are just another language feature, in reality, they will change the way you think about programming, offering a powerful new tool for applying abstraction to the programming challenges you face every day. Of course, Java already gives us powerful tools for abstraction, such as inheritance and generics, but these are largely about abstracting over data. Lambda expressions complement these by giving us a better tool for abstracting over behavior.
In embracing lambda expressions, Java has taken a mild turn towards functional programming. While it might seem that object-oriented programming and functional programming are competing disciplines, they offer us complementary tools for managing program complexity. And, as hardware parallelism continues to increase, the building blocks of functional programmingimmutable values and pure functionsbecome even more effective tools for managing that complexity.