Chapter 2. Passing code with behavior parameterization
Listing 2.1. Behavior parameterization: filtering apples with predicates
Chapter 3. Lambda expressions
Listing 3.1. Valid lambda expressions in Java 8
Listing 3.2. Working with a Predicate
Chapter 4. Introducing streams
Listing 4.1. Collections: external iteration with a for-each loop
Listing 4.2. Collections: external iteration using an iterator behind the scenes
Chapter 5. Working with streams
Listing 5.1. Finds all transactions in 2011 and sort by value (small to high)
Listing 5.2. What are all the unique cities where the traders work?
Listing 5.3. Finds all traders from Cambridge and sort them by name
Listing 5.4. Returns a string of all traders’ names sorted alphabetically
Listing 5.5. Are any traders based in Milan?
Listing 5.6. Prints all transactions’ values from the traders living in Cambridge
Listing 5.7. What’s the highest value of all the transactions?
Chapter 6. Collecting data with streams
Listing 6.1. Grouping transactions by currency in imperative style
Listing 6.2. Multilevel grouping
Listing 6.3. Finding the highest-calorie dish in each subgroup
Listing 6.4. The Collector interface
Listing 6.5. The ToListCollector
Listing 6.6. Partitioning the first n natural numbers into primes and nonprimes
Chapter 7. Parallel data processing and performance
Listing 7.1. Measuring performance of a function summing the first n numbers
Listing 7.2. Executing a parallel sum using the fork/join framework
Listing 7.3. The Spliterator interface
Listing 7.4. An iterative word counter method
Listing 7.5. A class to count words while traversing a stream of Characters
Chapter 10. Domain-specific languages using lambdas
Listing 10.1. Reading the error lines in a log file in imperative style
Listing 10.2. Reading the error lines in a log file in functional style
Listing 10.3. A fluent grouping collectors builder
Listing 10.4. Creating a stock trading order by using the domain object’s API directly
Listing 10.5. Creating a stock trading order with method chaining
Listing 10.6. An order builder providing a method-chaining DSL
Listing 10.7. Creating a stock-trading order with nested functions
Listing 10.8. An order builder providing a nested-function DSL
Listing 10.9. Creating a stock-trading order with function sequencing
Listing 10.10. An order builder providing a function-sequencing DSL
Listing 10.11. Creating a stock-trading order by using multiple DSL patterns
Listing 10.12. An order builder providing a DSL that mixes multiple styles
Listing 10.13. The taxes that can be applied to the order’s net value
Listing 10.14. Applying taxes to the order’s net value with a set of Boolean flags
Listing 10.15. A tax calculator that fluently defines the taxes to be applied
Listing 10.16. A tax calculator that fluently combines the tax functions to be applied
Listing 10.17. Selecting books from a database by using the jOOQ DSL
Listing 10.18. Implementing a test scenario by using Cucumber annotations
Listing 10.19. Configuring a Spring Integration flow by using the Spring Integration DSL
Chapter 11. Using Optional as a better alternative to null
Listing 11.1. The Person/Car/Insurance data model
Listing 11.2. Null-safe attempt 1: deep doubts
Listing 11.3. Null-safe attempt 2: too many exits
Listing 11.4. Redefining the Person/Car/Insurance data model by using Optional
Listing 11.5. Finding a car’s insurance company name with Optionals
Listing 11.6. Finding distinct insurance company names used by a list of persons
Listing 11.7. Converting a String to an Integer returning an optional
Chapter 12. New Date and Time API
Listing 12.1. Creating a LocalDate and reading its values
Listing 12.2. Reading LocalDate values by using a TemporalField
Listing 12.3. Creating a LocalTime and reading its values
Listing 12.4. Creating a LocalDateTime directly or by combining a date and a time
Listing 12.5. Creating Durations and Periods
Listing 12.6. Manipulating the attributes of a LocalDate in an absolute way
Listing 12.7. Manipulating the attributes of a LocalDate in a relative way
Listing 12.8. Using the predefined TemporalAdjusters
Listing 12.9. The TemporalAdjuster interface
Listing 12.10. Creating a DateTimeFormatter from a pattern
Listing 12.11. Creating a localized DateTimeFormatter
Chapter 16. CompletableFuture: composable asynchronous programming
Listing 16.1. Executing a long-lasting operation asynchronously in a Future
Listing 16.2. A method to simulate a 1-second delay
Listing 16.3. Introducing a simulated delay in the getPrice method
Listing 16.4. Implementing the getPriceAsync method
Listing 16.5. Using an asynchronous API
Listing 16.6. Propagating an error inside the CompletableFuture
Listing 16.7. Creating a CompletableFuture with the supplyAsync factory method
Listing 16.8. A findPrices implementation sequentially querying all the shops
Listing 16.9. Checking findPrices correctness and performance
Listing 16.10. Parallelizing the findPrices method
Listing 16.11. Implementing the findPrices method with CompletableFutures
Listing 16.12. A custom Executor fitting the best-price-finder application
Listing 16.13. An enumeration defining the discount codes
Listing 16.14. The Discount service
Listing 16.15. Simplest findPrices implementation that uses the Discount service
Listing 16.16. Implementing the findPrices method with CompletableFutures
Listing 16.17. Combining two independent CompletableFutures
Listing 16.18. Combining two Futures in Java 7
Listing 16.19. Adding a timeout to CompletableFuture
Listing 16.20. Completing a CompletableFuture with a default value after a timeout
Listing 16.21. A method to simulate a random delay between 0.5 and 2.5 seconds
Listing 16.22. Refactoring the findPrices method to return a stream of Futures
Chapter 17. Reactive programming
Listing 17.1. The Flow.Publisher interface
Listing 17.2. The Flow.Subscriber interface
Listing 17.3. The Flow.Subscription interface
Listing 17.4. The Flow.Processor interface
Listing 17.5. A Java bean conveying the currently reported temperature
Listing 17.6. A Subscription sending a stream of TempInfo to its Subscriber
Listing 17.7. A Subscriber printing the received temperatures
Listing 17.8. A main class: creating a Publisher and subscribing TempSubscriber to it
Listing 17.9. Adding an Executor to the TempSubscription
Listing 17.10. A Processor transforming temperatures from Fahrenheit to Celsius
Listing 17.11. Main class: create a Publisher and subscribe TempSubscriber to it
Listing 17.12. Creating an Observable emitting temperature once a second
Listing 17.13. An Observer printing the received temperatures
Listing 17.14. A main class printing the temperatures in New York
Listing 17.15. Using map on Observable to transform Fahrenheit into Celsius
Listing 17.16. Merging the temperatures reported for one or more towns
Listing 17.17. A main class printing the temperatures in three towns
Chapter 18. Thinking functionally
Listing 18.1. Iterative factorial
Listing 18.2. Recursive factorial
Chapter 19. Functional programming techniques
Listing 19.1. Implementing pattern matching to simplify an expression
Appendix B. Miscellaneous library updates
Listing B.1. LongAdder to calculate the sum of values
Listing B.2. LongAccumulator to calculate the sum of values
Listing B.3. parallelPrefix cumulates in parallel elements of an array
Appendix C. Performing multiple operations in parallel on a stream
Listing C.1. Defining a StreamForker to execute multiple operations on a stream
Listing C.2. The build method used to create ForkingStreamConsumer
Listing C.3. Futures created with the getOperationResult method
Listing C.4. A ForkingStreamConsumer to add stream elements to multiple queues
Listing C.5. A Spliterator reading the elements it traverses from a BlockingQueue
Appendix D. Lambdas and JVM bytecode
Listing D.1. A Function implemented as an anonymous inner class
Listing D.2. A Function implemented with a lambda expression