Java SE 8for the Really ImpatientThis page intentionally left blankJava se 8for the Really ImpatientCay S Horstmann六 Addison- Wesleypper Saddle river,NJ· Boston。IndlanapollsSan franciscoNew york· Toronto· Montreal· London· Munich· Paris· MadridCapetown· Sydney· Tokyo· Singapore· Mexico CityMany of the designations used by manufacturers and sellers to distinguish theirproducts are claimed as trademarks. Where those designations appear in this book,and the publisher was aware of a trademark claim, the designations have been printedwith initial capital letters or in all capitalsThe author and publisher have taken care in the preparation of this book, but makeno expressed or implied warranty of any kind and assume no responsibility for errorsor omissions. no liability is assumed for incidental or consequential damages inconnection with or arising out of the use of the information or programs containedherein.For information about buying this title in bulk quantities, or for special salesopportunities(which may include electronic versions; custom cover designs; andcontent particular to your business, training goals, marketing focus, or brandinginterests),pleasecontactourcorporatesalesdepartmentatcorpsales@pearsoned.comOr(800)382-3419Forgovernmentsalesinquiriespleasecontactgovernmentsales@pearsoned.comFor questions about sales outside the United States please contactinternational@pearsoned.comVisit us on the web: informit. com /awCataloging-in-Publication Data is on file with the Library of congressCopyright o 2014 Pearson Education IncAll rights reserved. Printed in the United States of America. This publication isprotected by copyright, and permission must be obtained from the publisher priorto any prohibited reproduction, storage in a retrieval system, or transmission in anyform or by any means, electronic, mechanical, photocopying, recording or likewiTo obtain permission to use material from this work please submit a written requesto Pearson Education, Inc, Permissions Department, One Lake Street, Upper SaddleRiver, New Jersey 07458, or you may fax your request to(201)236-3290ISBN-13:978-0-321-92776-7ISBN-10:0-32192776-1Text printed in the United States on recycled paper at Rr Donnelley in Crawfordsville,ndianaFirst printing, January 2014To Greg doench, my editor for two decades whose patience, kindnessand good judgment i greatly admireThis page intentionally left blankContentsPrefaceAbout the author1 LAMBDA EXPRESSIONS 1Why Lambdas1.2 The Syntax of Lambda Expressions1.3 Functional Interfaces 61.4 Method References 81.5 Constructor references g1.6 Variable Scope 107 Default methods 141. 8 Static Methods in Interfaces 16Exercises 18THE STREAM API 212.1From Iteration to Stream Operations222.2 Stream Creation 242.3 The filter, map and flatMap Methods 252.4 Extracting Substreams and Combining Streams 26Contents2.5 Stateful Transformations 272.6 Simple reductions82.7 The Optional Type292.7.1 Workth Optional values 2 92.7.2 Creating Optional Values 302.7.3 Composing Optional value functions with flatmar302.8 Reduction Operations2. 9 Collecting Results 332. 10 Collecting into maps 342. 11 Grouping and Partitioning 362.12 Primitive Type streams 392.13 Parallel Streams 402.14 Functional Interfaces 42Exercises 443 PROGRAMMING WITH LAMBDAS 473. 1 Deferred Execution 483.2 Parameters of Lambda Expressions 493. 3 Choosing a Functional Interface 503. 4 Returning Functions 533.5 Composition 543.6 Laziness 563.7 Parallelizing Operations 573. 8 Dealing with Exceptions 583.9 Lambdas and Generics613.10 Monadic Operations 63Exercises4 JAVAFX 694.1 A Brief History of Java GUl Programming 704.2 Hello, JavaFX!4.3 Event Handling 724.4 JavaFX Properties 734.5 Bindings 75ContentsIK4.6 Layout 804.7 FXML 864.8CSS904.9 Animations and Special Effects 914.10 Fancy Controls 94Exercises 975 THE NEW DATE AND TIME API 1015.1 The Time Line 1025.2 Local Dates 1045.3 Date Adjusters 1075.4 Local Time 1085.5 Zoned Time 1096 Formatting and Parsing 112Interoperating with Legacy Code 115Exercises116CONCURRENCY ENHANCEMENTS 11 96. 1 Atomic Values 1206.2 ConcurrentHashMap Improvements 1236.2.1 Updating Values 1246.2.2 Bulk Operations 1266.2. 3 Set Views 1286.3 Parallel Array Operations 1286.4 Completable futures 1306.4.1 Futures 1306.4.2 Composing Futures 1306.4.3 The Composition Pipeline 1316.4.4 Composing Asynchronous Operations 132Exercises 1347 THE NASHORN JAVASCRIPT ENGINE 1377.1 Running Nashorn from the Command Line 1387.2 Running Nashorn from Java39