O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modern Java Recipes

Book Description

The introduction of functional programming concepts in Java SE 8 was a drastic change for this venerable object-oriented language. Lambda expressions, method references, and streams fundamentally changed the idioms of the language, and many developers have been trying to catch up ever since. This cookbook will help. With more than 70 detailed recipes, author Ken Kousen shows you how to use the newest features of Java to solve a wide range of problems.

For developers comfortable with previous Java versions, this guide covers nearly all of Java SE 8, and includes a chapter focused on changes coming in Java 9. Need to understand how functional idioms will change the way you write code? This cookbook—chock full of use cases—is for you.

Recipes cover:

  • The basics of lambda expressions and method references
  • Interfaces in the java.util.function package
  • Stream operations for transforming and filtering data
  • Comparators and Collectors for sorting and converting streaming data
  • Combining lambdas, method references, and streams
  • Creating instances and extract values from Java’s Optional type
  • New I/O capabilities that support functional streams
  • The Date-Time API that replaces the legacy Date and Calendar classes
  • Mechanisms for experimenting with concurrency and parallelism

Table of Contents

  1. Foreword
  2. Preface
    1. Modern Java
    2. Who Should Read This Book
    3. How This Book Is Organized
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Safari
    7. How to Contact Us
    8. Acknowledgments
  3. The Basics
    1. 1.1. Lambda Expressions
    2. 1.2. Method References
    3. 1.3. Constructor References
    4. 1.4. Functional Interfaces
    5. 1.5. Default Methods in Interfaces
    6. 1.6. Static Methods in Interfaces
  4. The java.util.function Package
    1. 2.1. Consumers
    2. 2.2. Suppliers
    3. 2.3. Predicates
    4. 2.4. Functions
  5. Streams
    1. 3.1. Creating Streams
    2. 3.2. Boxed Streams
    3. 3.3. Reduction Operations Using Reduce
    4. 3.4. Check Sorting Using Reduce
    5. 3.5. Debugging Streams with peek
    6. 3.6. Converting Strings to Streams and Back
    7. 3.7. Counting Elements
    8. 3.8. Summary Statistics
    9. 3.9. Finding the First Element in a Stream
    10. 3.10. Using anyMatch, allMatch, and noneMatch
    11. 3.11. Stream flatMap Versus map
    12. 3.12. Concatenating Streams
    13. 3.13. Lazy Streams
  6. Comparators and Collectors
    1. 4.1. Sorting Using a Comparator
    2. 4.2. Converting a Stream into a Collection
    3. 4.3. Adding a Linear Collection to a Map
    4. 4.4. Sorting Maps
    5. 4.5. Partitioning and Grouping
    6. 4.6. Downstream Collectors
    7. 4.7. Finding Max and Min Values
    8. 4.8. Creating Immutable Collections
    9. 4.9. Implementing the Collector Interface
  7. Issues with Streams, Lambdas, and Method References
    1. 5.1. The java.util.Objects Class
    2. 5.2. Lambdas and Effectively Final
    3. 5.3. Streams of Random Numbers
    4. 5.4. Default Methods in Map
    5. 5.5. Default Method Conflict
    6. 5.6. Iterating Over Collections and Maps
    7. 5.7. Logging with a Supplier
    8. 5.8. Closure Composition
    9. 5.9. Using an Extracted Method for Exception Handling
    10. 5.10. Checked Exceptions and Lambdas
    11. 5.11. Using a Generic Exception Wrapper
  8. The Optional Type
    1. 6.1. Creating an Optional
    2. 6.2. Retrieving Values from an Optional
    3. 6.3. Optional in Getters and Setters
    4. 6.4. Optional flatMap Versus map
    5. 6.5. Mapping Optionals
  9. File I/O
    1. 7.1. Process Files
    2. 7.2. Retrieving Files as a Stream
    3. 7.3. Walking the Filesystem
    4. 7.4. Searching the Filesystem
  10. The java.time Package
    1. 8.1. Using the Basic Date-Time Classes
    2. 8.2. Creating Dates and Times from Existing Instances
    3. 8.3. Adjusters and Queries
    4. 8.4. Convert from java.util.Date to java.time.LocalDate
    5. 8.5. Parsing and Formatting
    6. 8.6. Finding Time Zones with Unusual Offsets
    7. 8.7. Finding Region Names from Offsets
    8. 8.8. Time Between Events
  11. Parallelism and Concurrency
    1. 9.1. Converting from Sequential to Parallel Streams
    2. 9.2. When Parallel Helps
    3. 9.3. Changing the Pool Size
    4. 9.4. The Future Interface
    5. 9.5. Completing a CompletableFuture
    6. 9.6. Coordinating CompletableFutures, Part 1
    7. 9.7. Coordinating CompletableFutures, Part 2
  12. Java 9 Additions
    1. 10.1. Modules in Jigsaw
    2. 10.2. Private Methods in Interfaces
    3. 10.3. Creating Immutable Collections
    4. 10.4. Stream: ofNullable, iterate, takeWhile, and dropWhile
    5. 10.5. Downstream Collectors: filtering and flatMapping
    6. 10.6. Optional: stream, or, ifPresentOrElse
    7. 10.7. Date Ranges
  13. Generics and Java 8
    1. Background
    2. What Everybody Knows
    3. What Some Developers Don’t Realize
    4. Wildcards and PECS
    5. Examples from the Java 8 API
    6. Summary
  14. Index