Functional Interfaces in Java - Ralph Lecessi

Functional Interfaces in Java (eBook)

Fundamentals and Examples

(Autor)

eBook Download: PDF
2019 | 1st ed.
XVII, 410 Seiten
Apress (Verlag)
978-1-4842-4278-0 (ISBN)
Systemvoraussetzungen
66,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen
Reduce development time by organizing your programs as chains of functional interfaces and see that the advantages of using functional interfaces include the flexibility and power of inlined functional chains and reuse of functional methods utilized throughout the Java API. You'll see how complex logical expressions can be reduced to chains of predicates and how chains of comparators can be used to sort data by several criteria in order. Other examples include streams that utilize functional interfaces to filter, sort, transform, and perform calculations on data; CompletableFutures that use functional interfaces to create cascading and parallel execution threads; and JavaFX programs that use functional interfaces to monitor the data backed by their graphical components.

Each chapter contains a complete programming project: the Discount Dave project shows you how to qualify car customers by organizing questions as a list of predicates; the Real Estate Broker project shows you how to use chains of comparators to filter and sort homes according to customer priorities; the Dave's Part Inventory project shows you how to query and write reports from an inventory database using stream operations; and the Sentence Builder project shows you how to correct a sentence by implementing each grammar rule as a separate link in a future chain.

Functional Interfaces in Java will help you quickly develop powerful and reliable programs that utilize functional interfaces to implement logic and calculations.


What You Will Learn
  • Use the functional interfaces in the java.util.function package to perform conditional logic, transform and generate data, and perform calculations
  • Filter and sort data by several criteria  using comparators 
  • Process collections and filter, sort, transform, and reduce stream elements with functional interfaces 
  • Write cascading and parallel execution threads

Who This Book Is For

Computer science student or a professional Java programmer. This work is a rigorous discussion of the application of functional interfaces, so prerequisites for this text include basic Java programming and object-oriented Java programming.



Ralph Lecessi, PhD, is a software engineer with over thirty years professional programming experience in the aerospace, telecommunications, and payment industries at companies including Lockheed Martin, Alcatel-Lucent, AT&T, and Northrop Grumman.  He is also an adjunct professor of computer science and Java programming.

Reduce development time by organizing your programs as chains of functional interfaces and see that the advantages of using functional interfaces include the flexibility and power of inlined functional chains and reuse of functional methods utilized throughout the Java API. You'll see how complex logical expressions can be reduced to chains of predicates and how chains of comparators can be used to sort data by several criteria in order. Other examples include streams that utilize functional interfaces to filter, sort, transform, and perform calculations on data; CompletableFutures that use functional interfaces to create cascading and parallel execution threads; and JavaFX programs that use functional interfaces to monitor the data backed by their graphical components.Each chapter contains a complete programming project: the Discount Dave project shows you how to qualify car customers by organizing questions as a list of predicates; the Real Estate Broker project shows you how to use chains of comparators to filter and sort homes according to customer priorities; the Dave's Part Inventory project shows you how to query and write reports from an inventory database using stream operations; and the Sentence Builder project shows you how to correct a sentence by implementing each grammar rule as a separate link in a future chain.Functional Interfaces in Java will help you quickly develop powerful and reliable programs that utilize functional interfaces to implement logic and calculations.What You Will LearnUse the functional interfaces in the java.util.function package to perform conditional logic, transform and generate data, and perform calculationsFilter and sort data by several criteria  using comparators Process collections and filter, sort, transform, and reduce stream elements with functional interfaces Write cascading and parallel execution threadsWho This Book Is ForComputer science student or a professional Java programmer. This work is a rigorous discussion of the application of functional interfaces, so prerequisites for this text include basic Java programming and object-oriented Java programming.

Ralph Lecessi is a software engineer with over 30 years’ professional programming experience in the aerospace, telecommunications, and payment industries at companies including Lockheed Martin, Alcatel-Lucent, AT&T, and Northrop Grumman. He is currently lead embedded software developer at TranSendIT, Inc in Mount Laurel, New Jersey.Ralph is also the author of JAVATM - The Beginnings—a text on basic Java programming that includes many examples and diagrams. Ralph is an adjunct professor of programming at Middlesex County College, where he teaches basic and object-oriented programming in Java. He lives in South Brunswick, New Jersey, with his wife and two children.

Table of Contents 4
About the Author 13
About the Technical Reviewer 14
Foreword 15
Chapter 1: Functional Interfaces 16
Section 1.1: Interfaces in Java 16
Section 1.2: Enhancements to Interfaces in Java 8 and Java 9 17
Section 1.3: Functional Interfaces Defined 20
Section 1.4: Implementing Functional Interfaces with Pre-Java 8 Constructs 21
Section 1.5: Providing Default Methods in  Functional Interfaces 23
Section 1.6: Providing Static Methods in Functional Interfaces 26
Section 1.7: Generic Functional Interfaces 28
Section 1.7.1: Generic Functional Interfaces with Type Restrictions 29
Section 1.8: Specializing a Generic Functional Interface 32
PROJECT 1: Playful Pets 35
Problem Statement 35
Solution 35
Short Problems 40
Long Problems 41
Chapter 2: Lambda Expressions 43
Section 2.1: Lambda Expressions Defined 43
Section 2.2: Using Lambda Expressions to  Represent Functional Interfaces 43
Section 2.3: The Scope of a Lambda Expression 45
Section 2.4: Lambda Argument List Variations 46
Section 2.5: Lambda Bodies in Block Form 48
Section 2.6: Limitations of Lambda Expressions 51
PROJECT 2: Compute Square 54
Problem Statement 54
Solution 54
Short Problems 57
Long Problems 57
Chapter 3: Predicates 58
Section 3.1: The java.util.function Package 58
Section 3.2: The Predicate Interface 58
Section 3.3: Passing a Predicate to a Method 60
Section 3.4: Chains of Functional Interfaces 63
Section 3.5: Predicate Chaining Creates Complex Logical Expressions 63
Section 3.5.1: Chains Involving the OR Operation 63
Section 3.5.2: Chains Involving the AND Operation 65
Section 3.5.3: Chains Involving the ! Operation 66
Section 3.5.4: Using Predicate.isEqual 67
Section 3.5.5: Using Predicate.not [JAVA 11] 68
Section 3.6: Overriding Predicate Default Methods 69
Section 3.7: Specializations of Predicates 70
Section 3.8: Binary Predicates 72
PROJECT 3: Discount Dave 72
Problem Statement 72
Solution 73
Short Problems 80
Long Problems 80
Chapter 4: Functions 82
Section 4.1: The Function Interface 82
Section 4.2: Passing a Generic Function to a Method 83
Section 4.2.1: Passing a Function with Restricted or Known Type Parameters 84
Section 4.3: Function Chaining 86
Section 4.3.1: Chains Involving the andThen Method 86
Section 4.3.2: Chains Involving the compose Method 86
Section 4.4: The Function.identity Method 87
Section 4.5: Specializations of Functions Which Convert from Primitive Types 88
Section 4.6: Specializations of Functions Which Convert to Primitive Types 89
Section 4.7: Non-generic Specializations of Functions 90
Section 4.8: Binary Functions 90
Section 4.9: Creating Chains Using BiFunctions 91
Section 4.10: Specializations of BiFunctions Which Convert to Primitive Types 92
PROJECT 4: Sales Promotions 93
Problem Statement 93
Solution 93
Short Problems 105
Long Problems 106
Chapter 5: Operators 107
Section 5.1: The UnaryOperator Interface 107
Section 5.2: Specializations of UnaryOperator 108
Section 5.2.1: Chains Involving UnaryOperator Specializations 109
Section 5.3: The BinaryOperator Interface 110
Section 5.4: Non-generic Specializations of  BinaryOperator 111
PROJECT 5: Calculator 112
Problem Statement 112
Solution 112
Short Problems 118
Long Problems 119
Chapter 6: Consumers 120
Section 6.1: The Consumer Interface 120
Section 6.2: Using Chains of Consumers to Compute Equations 121
Section 6.3: Using Consumers with println as the Terminal Operation 123
Section 6.4: Non-generic Specializations of Consumers 123
Section 6.5: The BiConsumer Interface 125
Section 6.6: Specializations of BiConsumer 126
PROJECT 6: Bank Transactions 127
Problem Statement 127
Solution 128
Short Problems 139
Long Problems 140
Chapter 7: Suppliers 141
Section 7.1: The Supplier Interface 141
Section 7.2: Wrapping User Prompts in a Supplier 143
Section 7.3: Non-generic Specializations of Suppliers 145
PROJECT 7: Ticketing System 146
Problem Statement 146
Solution 147
Short Problems 158
Long Problems 159
Chapter 8: Use in Traversing Objects 161
Section 8.1: Traversing Objects Using Iterators 161
Section 8.2: Traversing Java Arrays of Primitive Types 162
Section 8.2.1: Using Specializations of PrimitiveIterator 166
Section 8.3: Traversing Objects Using Spliterators 170
Section 8.4: Traversing Iterable Objects 174
Section 8.5: Traversing Iterable Objects That Contain Java Arrays of Primitives 175
Section 8.5.1: Using Specializations of PrimitiveIterator 179
Section 8.6: Traversing Maps 181
Section 8.7: Traversing Sets 182
PROJECT 8: Payroll 182
Problem Statement 182
Solution 183
Short Problems 189
Long Problems 190
Chapter 9: Use in Collections 191
Section 9.1: Removing Elements from a Collection 191
Section 9.2: Populating an Array 192
Section 9.3: Replacing the Elements of a List or a Map 194
Section 9.4: Parallel Computations on Arrays 195
Section 9.5: Map Computations 196
Section 9.6: Map Merging 199
Section 9.7: Functional Interfaces and Sets 201
PROJECT 9: Department of Motor Vehicles 201
Problem Statement 201
Solution 202
Short Problems 214
Long Problems 216
Chapter 10: Use in Comparing Objects 218
Section 10.1: The Comparator Interface 218
Section 10.2: Some Useful Comparator Methods 220
Section 10.3: The Comparator comparing Methods 221
Section 10.4: Specializations of the Comparator comparing Method 226
Section 10.5: Building Chains of Comparators 227
Section 10.6: Specializing Comparator Chain Components 230
Section 10.7: Using Comparators to Sort Lists 231
Section 10.8: Using Comparators to Sort Java Arrays 233
Section 10.9: Using Comparators to Organize Maps 236
Section 10.10: Using Comparators in BinaryOperator Methods 237
PROJECT 10: Real Estate Broker 238
Problem Statement 238
Solution 239
Short Problems 248
Long Problems 249
Chapter 11: Use in Optionals 252
Section 11.1: Creating an Optional 252
Section 11.2: Determining If an Optional Is Present 253
Section 11.3: Retrieving the Contents of an Optional 254
Section 11.4: Creating Chains of Optionals 255
Section 11.5: Printing the Contents of an Optional 257
Section 11.6: Filtering Optionals 258
Section 11.7: Optional Chains Involving map and flatmap 259
PROJECT 11: Guess a Number 262
Problem Statement 262
Solution 262
Short Problems 265
Long Problems 266
Chapter 12: Use in Streams 267
Section 12.1: Generating Stream Elements 267
Section 12.2: Traversing Streams 268
Section 12.3: Filtering Stream Elements 269
Section 12.4: Converting an Object to a Stream 270
Section 12.5: Sorting Stream Elements 270
Section 12.6: Selecting the Smallest or Largest Element in a Stream 271
Section 12.7: flatMap vs. map 272
Section 12.8: Reducing Stream Elements 274
Section 12.9: Collecting Stream Elements into a Mutable Reduction 275
Section 12.9.1: Using Prewritten Collectors 279
Section 12.10: Building Streams Interactively 284
Section 12.11: Displaying Intermediate Results 285
Section 12.12: Stream Specializations 286
PROJECT 12: Dave’s Part Inventory 287
Problem Statement 287
Solution 287
Short Problems 298
Long Problems 298
Chapter 13: Use in Multithreaded Programs 300
Section 13.1: Performing Computations Using Runnable and Callable 300
Section 13.1.1: Using Runnable in Optionals 304
Section 13.2: Futures and FutureTasks 304
Section 13.3: CompletionStages and  CompletableFutures 305
Section 13.4: Creating CompletableFutures and Retrieving Their Results 306
Section 13.5: Using the thenApply and thenAccept Methods 308
Section 13.6: Processing the First Asynchronous Future to Complete 309
Section 13.7: Making a Future Dependent on Another Future 310
Section 13.8: Cancelling a Future 311
Section 13.9: When Futures Throw Unchecked Exceptions 313
Section 13.10: Running Futures in Parallel 314
PROJECT 13: Sentence Builder 320
Problem Statement 320
Solution 320
Short Problems 333
Long Problems 334
Chapter 14: Use in Atomic Calculations 335
Section 14.1: Atomic Integers 336
Section 14.1.1: Accumulating an Atomic Value 338
Section 14.1.2: Updating an Atomic Value 339
Section 14.1.3: Comparing an Atomic Value 340
Section 14.2: Atomic Longs 343
Section 14.3: Atomic Booleans 344
Section 14.4: Atomic Arrays 344
Section 14.5: Atomic References 345
PROJECT 14: Bank Account 347
Problem Statement 347
Solution 347
Short Problems 351
Long Problems 352
Chapter 15: Use in JavaFX Applications 353
Section 15.1: Handling JavaFX Events 353
Section 15.2: Building JavaFX UI Components 355
Section 15.3: JavaFX Builder Factories 358
Section 15.4: Monitoring Changes in Collections 362
Section 15.4.1: Monitoring List Changes 362
Section 15.4.2: Monitoring Set Changes 368
Section 15.4.3: Monitoring Map Changes 372
Section 15.5: Invalidating an Observable Object 376
PROJECT 15: DMV GUI 380
Problem Statement 380
Solution 380
Short Problems 394
Long Problems 395
Appendix: Method References 397
Section A.1: Using References to Static Methods to Represent Functional Interfaces 397
Section A.2: References to Instance Methods 399
Section A.3: References to Constructors 400
Section A.4: Passing Method References as Arguments 401
Section A.5: Representing Functions with Method References 403
Section A.6: Using Method References with Comparators 405
Section A.7: Using Method References with Streams 406
Index 410

Erscheint lt. Verlag 14.2.2019
Zusatzinfo XVII, 410 p. 10 illus.
Verlagsort Berkeley
Sprache englisch
Themenwelt Informatik Programmiersprachen / -werkzeuge Java
Schlagworte Code • Computer Science • CS • Functional • interfaces • Java • programming • Software • source • Tech
ISBN-10 1-4842-4278-5 / 1484242785
ISBN-13 978-1-4842-4278-0 / 9781484242780
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 3,1 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Einführung, Ausbildung, Praxis

von Christian Ullenboom

eBook Download (2023)
Rheinwerk Computing (Verlag)
34,93
Moderne GUIs für RIAs und Java-Applikationen

von Ralph Steyer

eBook Download (2022)
Springer Fachmedien Wiesbaden (Verlag)
42,99