Table of Contents Foreword 1 Chapter 1 - Functional Interfaces 2 Section 1 - Interfaces in Java 2 Section 2 - Java 8 Enhancements to Interfaces 2 Section 3 - Functional Interfaces Defined 4 Section 4 - Implementing Functional Interfaces with Pre-Java 8 Constructs 5 Section 5 - Providing Default Methods in Functional Interfaces 6 Section 6 - Providing Static Methods in Functional Interfaces 9 Section 7 - Generic Functional Interfaces 10 Section 7a - Generic Functional Interfaces with Type Restrictions 12 Section 8 - Specializing a Generic Functional Interface 15 PROJECT 1 - Playful Pets 17 Short Problems 21 Long Problems 22 Chapter 2 - Lambda Expressions 24 Section 1 - Lambda Expressions Defined 24 Section 2 - Using Lambda Expressions to Represent Functional Interfaces 24 Section 3 - The Scope of a Lambda Expression 26 Section 4 - Lambda Argument List Variations 27 Section 5 - Lambda Bodies in Block Form 28 Section 6 - Limitations of Lambda Expressions 31 PROJECT 2 - Compute Square 33 Short Problems 35 Long Problems 36 Chapter 3 - Predicates 37 Section 1 - The java.util.function Package 37 Section 2 - The Predicate Interface 37 Section 3 - Passing a Predicate to a Method 39 Section 4 - Chains of Functional Interfaces 41 Section 5 - Predicate Chaining Creates Complex Logical Expressions 41 Section 5a - Chains involving the OR Operation 42 Section 5b - Chains involving the AND Operation 43 Section 5c - Chains involving the ! Operation 44 Section 5d - Using Predicate.isEqual 45 Section 5e - Using Predicate.not [JAVA 11] 46 Section 6- Overriding Predicate Default Methods 47 Section 7- Specializations of Predicates 48 Section 8- Binary Predicates 50 PROJECT 3 - Discount Dave 50 Short Problems 57 Long Problems 57 Chapter 4 - Functions 59 Section 1 - The Function Interface 59 Section 2 - Passing a Generic Function to a Method 60 Section 2a - Passing a Function with Restricted or Known Type Parameters 61 Section 3 - Function Chaining 63 Section 3a - Chains involving the andThen Method 63 Section 3b - Chains involving the compose Method 63 Section 4 - The Function.identity Method 64 Section 5- Specializations of Functions Which Convert from Primitive Types 65 Section 6- Specializations of Functions which Convert to Primitive Types 66 Section 7- Non Generic Specializations of Functions 67 Section 8- Binary Functions 67 Section 9 - Creating Chains using BiFunctions 68 Section 10 - Specializations of BiFunctions Which Convert to Primitive Types 69 PROJECT 4 - Sales Promotions 70 Short Problems 80 Long Problems 81 Chapter 5 - Operators 83 Section 1 - The UnaryOperator Interface 83 Section 2 - Specializations of UnaryOperator 84 Section 2a - Chains involving UnaryOperator Specializations 85 Section 3 - The BinaryOperator Interface 85 Section 4- Non Generic Specializations of BinaryOperator 86 PROJECT 5 - Calculator 87 Short Problems 93 Long Problems 93 Chapter 6 - Consumers 95 Section 1 - The Consumer Interface 95 Section 2 - Using Chains of Consumers to Compute Equations 96 Section 3 - Using Consumers with println as the Terminal Operation 98 Section 4 - Non Generic Specializations of Consumers 98 Section 5 - The BiConsumer Interface 99 Section 6 - Specializations of BiConsumer 100 PROJECT 6 - Bank Transactions 101 Short Problems 112 Long Problems 113 Chapter 7 - Suppliers 114 Section 1 - The Supplier Interface 114 Section 2 - Wrapping User Prompts in a Supplier 116 Section 3- Non Generic Specializations of Suppliers 117 PROJECT 7 - Ticketing System 119 Short Problems 130 Long Problems 130 Chapter 8 - Use in Traversing Objects 132 Section 1 - Traversing Objects using Iterators 132 Section 2 - Traversing Java Arrays of Primitive Types 133 Section 2a - Using Specializations of PrimitiveIterator 136 Section 3 - Traversing Objects using Spliterators 141 Section 4 - Traversing Iterable Objects 142 Section 5 - Traversing Iterable Objects the Contain Java Arrays of Primitives 143 Section 5a - Using Specializations of PrimitiveIterator 147 Section 6 - Traversing Maps 148 PROJECT 8 - Payroll 149 Short Problems 155 Long Problems 156 Chapter 9 - Use in Collections 157 Section 1 - Removing Elements from a Collection 157 Section 2 - Populating an Array 157 Section 3 - Replacing the Elements of a List or a Map 159 Section 4 - Parallel Computations on Arrays 160 Section 5 - Map Computations 161 Section 6 - Map Merging 164 PROJECT 9 - Department of Motor Vehicles 165 Short Problems 177 Long Problems 178 Chapter 10 - Use in Comparing Objects 180 Section 1 - The Comparator Interface 180 Section 2 - Some Useful Comparator Methods 181 Section 3 - The Comparator comparing Methods 183 Section 4 - Specializations of the Comparator comparing Method 186 Section 5 - Building Chains of Comparators 187 Section 6 - Specializing Comparator Chain Components 190 Section 7 - Using Comparators to Sort Lists 191 Section 8 - Using Comparators to Sort Java Arrays 193 Section 9 - Using Comparators to Organize Maps 195 Section 10 - Using Comparators in BinaryOperator Methods 196 PROJECT 10 - Real Estate Broker 197 Short Problems 206 Long Problems 207 Chapter 11 - Use in Optionals 209 Section 1 - Creating an Optional 209 Section 2 - Determining if an Optional is Present 210 Section 3 - Retrieving the Contents of an Optional 211 Section 4 - Creating Chains of Optionals 211 Section 5 - Printing the Contents of an Optional 214 Section 6 - Filtering Optionals 214 Section 7 - Optional Chains Involving map and flatmap 215 PROJECT 11 - Guess a Number 217 Short Problems 220 Long Problems 221 Chapter 12 - Use in Streams 222 Section 1 - Generating Stream Elements 222 Section 2 - Traversing Streams 223 Section 3 - Filtering Stream Elements 223 Section 4 - Converting an Object to a Stream 224 Section 5 - Sorting Stream Elements 225 Section 6 - Selecting the Smallest or Largest Element in a Stream 226 Section 7 - flatMap vs map 227 Section 8 - Reducing Stream Elements 228 Section 9 - Collecting Stream Elements into a Mutable Reduction 230 Section 9a - Using Pre-written Collectors 233 Section 10 - Building Streams Interactively 238 Section 11 - Displaying Intermediate Results 239 Section 12 - Stream Specializations 239 PROJECT 12 - Dave''s Part Inventory 241 Short Problems 250 Long Problems 250 Chapter 13 - Use in Multithreaded Programs 252 Section 1 - Performing Computations using Runnable and Callable 252 Section 1a - Using Runnable in Optionals 254 Section 2 - Futures and FutureTasks 254 Section 3 - CompletionStages and CompletableFutures 255 Section 4 - Creating CompletableFutures and Retrieving Their Results 256 Section 5 - Using the thenApply and thenAccept methods 257 Section 6 - Processing the first Asynchronous Future to Complete 258 Section 7 - Making a Future Dependent on Another Future 260 Section 8 - Cancelling a Future 261 Section 9 - When Futures throw Unchecked Exceptions 262 Section 10 - Running Futures in Parallel 263 PROJECT 13 - Sentence Builder 268 Short Problems 279 Long Problems 280 Chapter 14 - Use in Atomic Calculations 281 Section 1 - Atomic Integers 282 Section 1a - Accumulating an Atomic Value 283 Section 1b - Updating an Atomic Value 284 Section 1c - Comparing an Atomic Value 285 Section 2 - Atomic Longs 288 Section 3 - Atomic Booleans 289 Section 4 - Atomic Arrays 289 Section 5 - Atomic References 290 PROJECT 14 - Bank Account 292 Short Problems 296 Long Problems 296 Chapter 15 - Use in JavaFX Applications [JAVA9, JAVA10] 298 Section 1 - Handling JavaFX Events 298 Section 2 - Building JavaFX UI Components 300 Section 3 - JavaFX Builder Factories 302 Section 4 - Monitoring Changes in Collections 305 Section 4a - Monitoring List Changes 305 Section 4b - Monitoring Set Changes 310 Section 4c - Monitoring Map Changes 313 Section 5 - Invalidating an Observable Object 317 PROJECT 15 - DMV GUI 320 Short Problems 332 Long Problems 333.
Functional Interfaces in Java : Fundamentals and Examples