List of Listings

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

Listing 3.3. Working with a Consumer

Listing 3.4. Working with a Function

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

Listing 4.3. Streams: internal iteration

Chapter 5. Working with streams

Listing 5.1. Find 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. Find all traders from Cambridge and sort them by name

Listing 5.4. Return a string of all traders’ names sorted alphabetically

Listing 5.5. Are any traders based in Milan?

Listing 5.6. Print all transactions’ values from the traders living in Cambridge

Listing 5.7. What’s the highest value of all the transactions?

Listing 5.8. Find the transaction with the smallest value

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

Listing 6.7. The PrimeNumbersCollector

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

Listing 7.6. The WordCounterSpliterator

Chapter 10. Using Optional as a better alternative to null

Listing 10.1. The Person/Car/Insurance data model

Listing 10.2. Null-safe attempt 1: deep doubts

Listing 10.3. Null-safe attempt 2: too many exits

Listing 10.4. Redefining the Person/Car/Insurance data model using Optional

Listing 10.5. Finding a car’s insurance company name with Optionals

Listing 10.6. Converting a String into an Integer returning an optional

Listing 10.7. Reading duration from a property imperatively

Chapter 11. CompletableFuture: composable asynchronous programming

Listing 11.1. Executing a long-lasting operation asynchronously in a Future

Listing 11.2. A method to simulate a 1-second delay

Listing 11.3. Introducing a simulated delay in the getPrice method

Listing 11.4. Implementing the getPriceAsync method

Listing 11.5. Using an asynchronous API

Listing 11.6. Propagating an error inside the CompletableFuture

Listing 11.7. Creating a CompletableFuture with the supplyAsync factory method

Listing 11.8. A findPrices implementation sequentially querying all the shops

Listing 11.9. Checking findPrices correctness and performance

Listing 11.10. Parallelizing the findPrices method

Listing 11.11. Implementing the findPrices method with CompletableFutures

Listing 11.12. A custom Executor fitting our best-price-finder application

Listing 11.13. An enumeration defining the discount codes

Listing 11.14. The Discount service

Listing 11.15. Simplest findPrices implementation that uses the Discount service

Listing 11.16. Implementing the findPrices method with CompletableFutures

Listing 11.17. Combining two independent CompletableFutures

Listing 11.18. Combining two Futures in Java 7

Listing 11.19. A method to simulate a random delay between 0.5 and 2.5 seconds

Listing 11.20. Refactoring the findPrices method to return a stream of Futures

Listing 11.21. Reacting to CompletableFuture completion

Chapter 12. New Date and Time API

Listing 12.1. Creating a LocalDate and reading its values

Listing 12.2. Reading LocalDate values 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

Listing 12.12. Building a DateTimeFormatter

Listing 12.13. Applying a time zone to a point in time

Chapter 13. Thinking functionally

Listing 13.1. Iterative factorial

Listing 13.2. Recursive factorial

Listing 13.3. Stream factorial

Listing 13.4. Tail-recursive factorial

Chapter 14. Functional programming techniques

Listing 14.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

Listing C.6. Putting the StreamForker to work

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset