Table Of Contentwww.it-ebooks.info
Java 8 in Action: Lambdas, streams, and
functional-style programming
Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft
2
www.it-ebooks.info
Copyright
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
moreinformation,pleasecontact
SpecialSalesDepartment
ManningPublicationsCo.
20Baldwin Road
POBox761
ShelterIsland, NY11964
Email:[email protected]
©2015byManning PublicationsCo.Allrightsreserved.
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
permissionofthepublisher.
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
wasawareofatrademarkclaim, thedesignationshavebeen printedininitial capsorallcaps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to
have the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books are
printed on paper that is at least 15 percent recycled and processed without the use of elemental
chlorine.
Developmenteditor:Susan Conant
Manning PublicationsCo. Technicaldevelopment editor AlScherer
20Baldwin Road Copyeditor:LindaRecktenwald
POBox761 Proofreader:KatieTennant
Shelter Island,NY11964 Typesetter:DennisDalinnik
Coverdesigner:MariaTudor
3
www.it-ebooks.info
ISBN:9781617291999
Printedin theUnitedStatesofAmerica
12345678910–EBM–191817161514
4
www.it-ebooks.info
Dedication
To our parents
5
www.it-ebooks.info
Table of Contents
Copyright..............................................................................................................................................3
Dedication.............................................................................................................................................5
Part1. Fundamentals...................................................................................................................11
Chapter1. Java8: why should you care?............................................................................12
1.1.WhyisJavastillchanging?.................................................................................................14
1.2.Functionsin Java.................................................................................................................21
1.3.Streams.................................................................................................................................28
1.4.Defaultmethods..................................................................................................................32
1.5.Other goodideasfromfunctionalprogramming............................................................34
1.6.Summary...............................................................................................................................36
Chapter2. Passing code with behavior parameterization..........................................37
2.1.Coping withchanging requirements.................................................................................38
2.2.Behavior parameterization.................................................................................................41
2.3.Tackling verbosity...............................................................................................................47
2.4.Real-worldexamples..........................................................................................................52
2.5.Summary..............................................................................................................................54
Chapter3. Lambda expressions.............................................................................................56
3.1.Lambdasinanutshell.........................................................................................................57
3.2.Whereandhowtouselambdas........................................................................................60
3.3.Puttinglambdasintopractice:theexecutearoundpattern.........................................66
3.4.Usingfunctionalinterfaces................................................................................................70
3.5.Typechecking,typeinference,andrestrictions..............................................................76
3.6.Methodreferences..............................................................................................................82
3.7.Puttinglambdasandmethodreferencesintopractice!.................................................89
3.8.Usefulmethodstocompose lambdaexpressions...........................................................91
3.9.Similar ideasfrommathematics.......................................................................................95
3.10.Summary............................................................................................................................98
Part2. Functional-style dataprocessing............................................................................99
6
www.it-ebooks.info
Chapter4. Introducing streams..........................................................................................100
4.1.Whatarestreams?.............................................................................................................100
4.2.Gettingstartedwithstreams...........................................................................................105
4.3.Streamsvs.collections.....................................................................................................108
4.4.Stream operations..............................................................................................................113
4.5.Summary.............................................................................................................................117
Chapter5. Working with streams........................................................................................118
5.1.Filteringandslicing...........................................................................................................119
5.2.Mapping..............................................................................................................................123
5.3.Finding andmatching.......................................................................................................129
5.4.Reducing.............................................................................................................................132
5.5.Puttingitallintopractice.................................................................................................140
5.6.Numericstreams...............................................................................................................145
5.7.Building streams................................................................................................................152
5.8.Summary............................................................................................................................158
Chapter6. Collecting datawith streams...........................................................................159
6.1.Collectorsinanutshell.....................................................................................................160
6.2.Reducingandsummarizing.............................................................................................163
6.3.Grouping.............................................................................................................................172
6.4.Partitioning........................................................................................................................180
6.5.TheCollectorinterface.....................................................................................................186
6.6.Developing yourowncollectorforbetterperformance...............................................194
6.7.Summary............................................................................................................................202
Chapter7. Parallel data processing and performance...............................................203
7.1.Parallel streams.................................................................................................................204
7.2.Thefork/join framework..................................................................................................214
7.3.Spliterator..........................................................................................................................222
7.4.Summary............................................................................................................................232
Part3. Effective Java 8 programming..............................................................................233
Chapter8. Refactoring, testing, anddebugging...........................................................234
8.1.Refactoring forimprovedreadabilityandflexibility....................................................234
7
www.it-ebooks.info
8.2.Refactoringobject-orienteddesign patternswithlambdas.......................................242
8.3.Testing lambdas................................................................................................................253
8.4.Debugging..........................................................................................................................256
8.5.Summary............................................................................................................................261
Chapter9. Default methods..................................................................................................262
9.1.EvolvingAPIs.....................................................................................................................265
9.2.Defaultmethodsinanutshell.........................................................................................269
9.3.Usagepatternsfordefaultmethods...............................................................................272
9.4.Resolutionrules.................................................................................................................277
9.5.Summary............................................................................................................................284
Chapter10. Using Optionalas a better alternativeto null.......................................285
10.1.Howdoyoumodel theabsenceofavalue?.................................................................286
10.2.Introducing theOptionalclass.....................................................................................290
10.3.PatternsforadoptingOptional.....................................................................................292
10.4.Practicalexamplesofusing Optional...........................................................................303
10.5.Summary..........................................................................................................................307
Chapter11. CompletableFuture: composable asynchronousprogramming....309
11.1.Futures................................................................................................................................311
11.2.Implementing anasynchronousAPI.............................................................................314
11.3.Makeyourcodenon-blocking.......................................................................................320
11.4.Pipeliningasynchronoustasks......................................................................................328
11.5.ReactingtoaCompletableFuturecompletion.............................................................338
11.6.Summary...........................................................................................................................342
Chapter12. New Date andTime API..................................................................................343
12.1.LocalDate,LocalTime, Instant,Duration,andPeriod...............................................344
12.2.Manipulating,parsing,andformatting dates.............................................................350
12.3.Working withdifferenttimezonesandcalendars.....................................................358
12.4.Summary...........................................................................................................................361
Part4. Beyond Java8..............................................................................................................363
Chapter13. Thinking functionally......................................................................................364
13.1.Implementing andmaintainingsystems......................................................................364
8
www.it-ebooks.info
13.2.What’sfunctionalprogramming?.................................................................................368
13.3.Recursion vs.iteration....................................................................................................375
13.4.Summary..........................................................................................................................379
Chapter14. Functionalprogramming techniques.......................................................381
14.1.Functionseverywhere.....................................................................................................381
14.2.Persistentdatastructures..............................................................................................385
14.3.Lazyevaluationwithstreams........................................................................................392
14.4.Patternmatching.............................................................................................................401
14.5.Miscellany........................................................................................................................407
14.6.Summary..........................................................................................................................410
Chapter15. Blending OOPandFP: comparing Java8 andScala...........................412
15.1.IntroductiontoScala.......................................................................................................413
15.2.Functions..........................................................................................................................422
15.3.Classes andtraits.............................................................................................................427
15.4.Summary..........................................................................................................................429
Chapter16. Conclusions andwhere next for Java.......................................................431
16.1.Review ofJava8features................................................................................................431
16.2.What’saheadforJava?...................................................................................................435
16.3.Thefinalword..................................................................................................................446
Appendix A. Miscellaneouslanguageupdates...............................................................447
A.1.Annotations........................................................................................................................447
A.2.Generalizedtarget-typeinference..................................................................................450
Appendix B.Miscellaneous libraryupdates...................................................................452
B.1.Collections..........................................................................................................................452
B.2.Concurrency......................................................................................................................455
B.3.Arrays.................................................................................................................................458
B.4.NumberandMath............................................................................................................459
B.5.Files....................................................................................................................................460
B.6.Reflection..........................................................................................................................460
B.7.String..................................................................................................................................460
Appendix C.Performing multiple operationsin parallel on a stream................462
9
www.it-ebooks.info
C.1.Forkingastream...............................................................................................................462
C.2.Performanceconsiderations...........................................................................................472
Appendix D. LambdasandJVM bytecode.......................................................................473
D.1.Anonymousclasses...........................................................................................................473
D.2.Bytecodegeneration.........................................................................................................473
D.3.InvokeDynamic totherescue.........................................................................................475
D.4.Code-generationstrategies.............................................................................................477
Index...............................................................................................................................................479
10
www.it-ebooks.info
Description:Java 8 in Action: Lambdas, streams, and functional-style programming. Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft www.it-ebooks.info