Java continues to grow and evolve, and this cookbook continues to evolve in tandem. With this guide, you’ll get up to speed right away with hundreds of hands-on recipes across a broad range of Java topics. You’ll learn useful techniques for everything from string handling and functional programming to network communication.
Each recipe includes self-contained code solutions that you can freely use, along with a discussion of how and why they work. If you’re familiar with Java basics, this cookbook will bolster your knowledge of the language and its many recent changes, including how to apply them in your day-to-day development. This updated edition covers changes through Java 12 and parts of 13 and 14.
Recipes include:
• Methods for compiling, running, and debugging
• Packaging Java classes and building applications
• Manipulating, comparing, and rearranging text
• Regular expressions for string and pattern matching
• Handling numbers, dates, and times
• Structuring data with collections, arrays, and other types
• Object-oriented and functional programming techniques
• Input/output, directory, and filesystem operations
• Network programming on both client and server
• Processing JSON for data interchange
• Multithreading and concurrency
• Using Java in big data applications
• Interfacing Java with other languages
Author(s): Ian F. Darwin
Edition: 4
Publisher: O’Reilly Media
Year: 2020
Language: English
Commentary: Vector PDF
Pages: 638
City: Sebastopol, CA
Tags: Machine Learning; Data Science; Programming; Multithreading; Data Structures; Java; Functional Programming; Cookbook; R; JSON; Best Practices; Object-Oriented Programming; Packages; Network Programming
Cover
Copyright
Table of Contents
Preface
Who This Book Is For
What’s in This Book?
Organization of This Book
Java Books
General Programming Books
Design Books
Conventions Used in This Book
Programming Conventions
Typesetting Conventions
Code Examples
O’Reilly Online Learning
Comments and Questions
Acknowledgments
At O’Reilly
Technical Reviewers
Readers
Etc.
Book Production Software
Chapter 1. Getting Started: Compiling and Running Java
1.0 Introduction
1.1 Compiling and Running Java: Standard JDK
Problem
Solution
1.2 Compiling and Running Java: GraalVM for Better Performance
Problem
Solution
Discussion
1.3 Compiling, Running, and Testing with an IDE
Problem
Solution
Discussion
See Also
1.4 Exploring Java with JShell
Problem
Solution
Discussion
1.5 Using CLASSPATH Effectively
Problem
Solution
Discussion
1.6 Downloading and Using the Code Examples
Problem
Solution
Discussion
1.7 Automating Dependencies, Compilation, Testing, and Deployment with Apache Maven
Problem
Solution
Discussion
See Also
1.8 Automating Dependencies, Compilation, Testing, and Deployment with Gradle
Problem
Solution
Discussion
See Also
1.9 Dealing with Deprecation Warnings
Problem
Solution
Discussion
See Also
1.10 Maintaining Code Correctness with Unit Testing: JUnit
Problem
Solution
Discussion
See Also
1.11 Maintaining Your Code with Continuous Integration
Problem
Solution
Discussion
1.12 Getting Readable Stack Traces
Problem
Solution
Discussion
1.13 Finding More Java Source Code
Problem
Solution
Discussion
1.14 Finding Runnable Java Libraries
Problem
Solution
Discussion
Chapter 2. Interacting with the Environment
2.0 Introduction
2.1 Getting Environment Variables
Problem
Solution
Discussion
2.2 Getting Information from System Properties
Problem
Solution
Discussion
See Also
2.3 Dealing with Code That Depends on the Java Version or the Operating System
Problem
Solution
Discussion
2.4 Using Extensions or Other Packaged APIs
Problem
Solution
Discussion
2.5 Using the Java Modules System
Problem
Solution
Discussion
Chapter 3. Strings and Things
3.0 Introduction
3.1 Taking Strings Apart with Substrings or Tokenizing
Problem
Solution
Discussion
See Also
3.2 Putting Strings Together with StringBuilder
Problem
Solution
Discussion
3.3 Processing a String One Character at a Time
Problem
Solution
Discussion
3.4 Aligning, Indenting, and Unindenting Strings
Problem
Solution
Discussion
See Also
3.5 Converting Between Unicode Characters and Strings
Problem
Solution
Discussion
See Also
3.6 Reversing a String by Word or by Character
Problem
Solution
Discussion
3.7 Expanding and Compressing Tabs
Problem
Solution
Discussion
3.8 Controlling Case
Problem
Solution
See Also
3.9 Entering Nonprintable Characters
Problem
Solution
Discussion
3.10 Trimming Blanks from the End of a String
Problem
Solution
Discussion
3.11 Creating a Message with I18N Resources
Problem
Solution
3.12 Using a Particular Locale
Problem
Solution
Discussion
3.13 Creating a Resource Bundle
Problem
Solution
Discussion
3.14 Program: A Simple Text Formatter
3.15 Program: Soundex Name Comparisons
See Also
Chapter 4. Pattern Matching with Regular Expressions
4.0 Introduction
See Also
4.1 Regular Expression Syntax
Problem
Solution
Discussion
4.2 Using Regexes in Java: Test for a Pattern
Problem
Solution
Discussion
4.3 Finding the Matching Text
Problem
Solution
4.4 Replacing the Matched Text
Problem
Solution
Discussion
4.5 Printing All Occurrences of a Pattern
Problem
Solution
4.6 Printing Lines Containing a Pattern
Problem
Solution
Discussion
4.7 Controlling Case in Regular Expressions
Problem
Solution
4.8 Matching Accented, or Composite, Characters
Problem
Solution
Discussion
4.9 Matching Newlines in Text
Problem
Solution
Discussion
4.10 Program: Apache Logfile Parsing
4.11 Program: Full Grep
Chapter 5. Numbers
5.0 Introduction
See Also
5.1 Checking Whether a String Is a Valid Number
Problem
Solution
Discussion
See Also
5.2 Converting Numbers to Objects and Vice Versa
Problem
Solution
Discussion
5.3 Taking a Fraction of an Integer Without Using Floating Point
Problem
Solution
Discussion
5.4 Working with Floating-Point Numbers
Problem
Solution
Discussion
5.5 Formatting Numbers
Problem
Solution
See Also
5.6 Converting Among Binary, Octal, Decimal, and Hexadecimal
Problem
Solution
Discussion
5.7 Operating on a Series of Integers
Problem
Solution
Discussion
5.8 Formatting with Correct Plurals
Problem
Solution
See Also
5.9 Generating Random Numbers
Problem
Solution
See Also
5.10 Multiplying Matrices
Problem
Solution
Discussion
See Also
5.11 Using Complex Numbers
Problem
Solution
5.12 Handling Very Large Numbers
Problem
Solution
Discussion
5.13 Program: TempConverter
5.14 Program: Number Palindromes
See Also
Chapter 6. Dates and Times
6.0 Introduction
6.1 Finding Today’s Date
Problem
Solution
Discussion
6.2 Formatting Dates and Times
Problem
Solution
Discussion
6.3 Converting Among Dates/Times, YMDHMS, and Epoch Seconds
Problem
Solution
Discussion
6.4 Parsing Strings into Dates
Problem
Solution
Discussion
6.5 Difference Between Two Dates
Problem
Solution
Discussion
See Also
6.6 Adding to or Subtracting from a Date
Problem
Solution
Discussion
6.7 Handling Recurring Events
Problem
Solution
Discussion
6.8 Computing Dates Involving Time Zones
Problem
Solution
Discussion
6.9 Interfacing with Legacy Date and Calendar Classes
Problem
Solution
Discussion
Chapter 7. Structuring Data with Java
7.0 Introduction
7.1 Using Arrays for Data Structuring
Problem
Solution
Discussion
7.2 Resizing an Array
Problem
Solution
Discussion
7.3 The Collections Framework
Problem
Solution
Discussion
See Also
7.4 Like an Array, but More Dynamic
Problem
Solution
Discussion
See Also
7.5 Using Generic Types in Your Own Class
Problem
Solution
Discussion
7.6 How Shall I Iterate Thee? Let Me Enumerate the Ways
Problem
Solution
Discussion
7.7 Eschewing Duplicates with a Set
Problem
Solution
Discussion
7.8 Structuring Data in a Linked List
Problem
Solution
Discussion
7.9 Mapping with Hashtable and HashMap
Problem
Solution
Discussion
7.10 Storing Strings in Properties and Preferences
Problem
Solution
Discussion
7.11 Sorting a Collection
Problem
Solution
Discussion
7.12 Avoiding the Urge to Sort
Problem
Solution
Discussion
7.13 Finding an Object in a Collection
Problem
Solution
Discussion
7.14 Converting a Collection to an Array
Problem
Solution
Discussion
7.15 Making Your Data Iterable
Problem
Solution
Discussion
7.16 Using a Stack of Objects
Problem
Solution
Discussion
7.17 Multidimensional Structures
Problem
Solution
Discussion
7.18 Simplifying Data Objects with Lombok or Record
Problem
Solution
Discussion
See Also
7.19 Program: Timing Comparisons
Chapter 8. Object-Oriented Techniques
8.0 Introduction
Advice, or Mantras
8.1 Object Methods: Formatting Objects with toString(), Comparing with Equals
Problem
Solution
Discussion
Difficulties and Alternatives to Clone
8.2 Using Inner Classes
Problem
Solution
Discussion
8.3 Providing Callbacks via Interfaces
Problem
Solution
Discussion
8.4 Polymorphism/Abstract Methods
Problem
Solution
Discussion
8.5 Using Typesafe Enumerations
Problem
Solution
Discussion
8.6 Avoiding NPEs with Optional
Problem
Solution
Discusssion
8.7 Enforcing the Singleton Pattern
Problem
Solution
Discussion
See Also
8.8 Roll Your Own Exceptions
Problem
Solution
Discussion
See Also
8.9 Using Dependency Injection
Problem
Solution
Discussion
See Also
8.10 Program: Plotter
Chapter 9. Functional Programming Techniques: Functional Interfaces, Streams, and Parallel Collections
9.0 Introduction
See Also
9.1 Using Lambdas/Closures Instead of Inner Classes
Problem
Solution
Discussion
9.2 Using Lambda Predefined Interfaces Instead of Your Own
Problem
Solution
Discussion
Roll Your Own Functional Interface
9.3 Simplifying Processing with Streams
Problem
Solution
Discussion
9.4 Simplifying Streams with Collectors
Problem
Solution
Discussion
9.5 Improving Throughput with Parallel Streams and Collections
Problem
Solution
Discussion
9.6 Using Existing Code as Functional with Method References
Problem
Solution
Discussion
9.7 Java Mixins: Mixing in Methods
Problem
Solution
Discussion
Chapter 10. Input and Output: Reading, Writing, and Directory Tricks
10.0 Introduction
10.1 About InputStreams/OutputStreams and Readers/Writers
See Also
10.2 Reading a Text File
Problem
Solution
Discussion
See Also
10.3 Reading from the Standard Input or from the Console/Controlling Terminal
Problem
Solution
Discussion
The Console (Controlling Terminal)
10.4 Printing with Formatter and printf
Problem
Solution
Discussion
10.5 Scanning Input with StreamTokenizer
Problem
Solution
Discussion
10.6 Scanning Input with the Scanner Class
Problem
Solution
Discussion
10.7 Scanning Input with Grammatical Structure
Problem
Solution
Discussion
10.8 Copying a File
Problem
Solution
Discussion
10.9 Reassigning the Standard Streams
Problem
Solution
Discussion
10.10 Duplicating a Stream as It Is Written; Reassigning Standard Streams
Problem
Solution
Discussion
10.11 Reading/Writing a Different Character Set
Problem
Solution
Discussion
10.12 Those Pesky End-of-Line Characters
Problem
Solution
Discussion
10.13 Beware Platform-Dependent File Code
Problem
Solution
Discussion
10.14 Reading/Writing Binary Data
Problem
Solution
Discussion
10.15 Reading and Writing JAR or ZIP Archives
Problem
Solution
Discussion
See Also
10.16 Finding Files in a Filesystem-Neutral Way with getResource() and getResourceAsStream()
Problem
Solution
Discussion
10.17 Getting File Information: Files and Path
Problem
Solution
Discussion
10.18 Creating a New File or Directory
Problem
Solution
Discussion
10.19 Changing a File’s Name or Other Attributes
Problem
Solution
Discussion
10.20 Deleting a File
Problem
Solution
Discussion
10.21 Creating a Transient/Temporary File
Problem
Solution
Discussion
10.22 Listing a Directory
Problem
Solution
Discussion
10.23 Getting the Directory Roots
Problem
Solution
Discussion
10.24 Using the FileWatcher Service to Get Notified About File Changes
Problem
Solution
Discussion
10.25 Program: Save User Data to Disk
Problem
Solution
Discussion
Acknowledgments
10.26 Program: Find—Walking a File Tree
Chapter 11. Data Science and R
11.1 Machine Learning with Java
Problem
Solution
Discussion
See Also
11.2 Using Data In Apache Spark
Problem
Solution
Discussion
11.3 Using R Interactively
Problem
Solution
Discussion
11.4 Comparing/Choosing an R Implementation
Problem
Solution
Discussion
11.5 Using R from Within a Java App: Renjin
Problem
Solution
Discussion
11.6 Using Java from Within an R Session
Problem
Solution
Discussion
11.7 Using FastR, the GraalVM Implementation of R
Problem
Solution
Discussion
11.8 Using R in a Web App
Problem
Solution
Discussion
Chapter 12. Network Clients
12.0 Introduction
12.1 HTTP/REST Web Client
Problem
Solution
Discussion
See Also
12.2 Contacting a Socket Server
Problem
Solution
Discussion
See Also
12.3 Finding and Reporting Network Addresses
Problem
Solution
Discussion
See Also
12.4 Handling Network Errors
Problem
Solution
12.5 Reading and Writing Textual Data
Problem
Solution
Discussion
12.6 Reading and Writing Binary or Serialized Data
Problem
Solution
Discussion
12.7 UDP Datagrams
Problem
Solution
Discussion
12.8 URI, URL, or URN?
Problem
Solution
Discussion
12.9 Program: TFTP UDP Client
12.10 Program: Sockets-Based Chat Client
See Also
12.11 Program: Simple HTTP Link Checker
Chapter 13. Server-Side Java
13.0 Introduction
13.1 Opening a Server Socket for Business
Problem
Solution
Discussion
13.2 Finding Network Interfaces
Problem
Solution
Discussion
13.3 Returning a Response (String or Binary)
Problem
Solution
Discussion
13.4 Returning Object Information Across a Network Connection
Problem
Solution
Discussion
13.5 Handling Multiple Clients
Problem
Solution
Discussion
13.6 Serving the HTTP Protocol
Problem
Solution
Discussion
13.7 Securing a Web Server with SSL and JSSE
Problem
Solution
Discussion
See Also
13.8 Creating a REST Service with JAX-RS
Problem
Solution
Discussion
13.9 Network Logging
Problem
Solution
Discussion
13.10 Setting Up SLF4J
Problem
Solution
Discussion
See Also
13.11 Network Logging with Log4j
Problem
Solution
Discussion
13.12 Network Logging with java.util.logging
Problem
Solution
Discussion
See Also
Chapter 14. Processing JSON Data
14.0 Introduction
14.1 Generating JSON Directly
Problem
Solution
Discussion
14.2 Parsing and Writing JSON with Jackson
Problem
Solution
Discussion
14.3 Parsing and Writing JSON with org.json
Problem
Solution
Discussion
See Also
14.4 Parsing and Writing JSON with JSON-B
Problem
Solution
Discussion
See Also
14.5 Finding JSON Elements with JSON Pointer
Problem
Solution
Discussion
See Also
Summary
Chapter 15. Packages and Packaging
15.0 Introduction
15.1 Creating a Package
Problem
Solution
Discussion
15.2 Documenting Classes with Javadoc
Problem
Solution
Discussion
See Also
Javadoc Versus JavaHelp
15.3 Beyond Javadoc: Annotations/Metadata
Problem
Solution
Discussion
15.4 Preparing a Class as a JavaBean
Problem
Solution
Discussion
15.5 Archiving with JAR
Problem
Solution
Discussion
15.6 Running a Program from a JAR
Problem
Solution
Discussion
15.7 Packaging Web Tier Components into a WAR File
Problem
Solution
Discussion
See Also
15.8 Creating a Smaller Distribution with jlink
Problem
Solution
Discussion
15.9 Using JPMS to Create a Module
Problem
Solution
Discussion
See Also
Chapter 16. Threaded Java
16.0 Introduction
16.1 Running Code in a Different Thread
Problem
Solution
Discussion
16.2 Displaying a Moving Image with Animation
Problem
Solution
Discussion
16.3 Stopping a Thread
Problem
Solution
Discussion
16.4 Rendezvous and Timeouts
Problem
Solution
Discussion
16.5 Synchronizing Threads with the synchronized Keyword
Problem
Solution
Discussion
16.6 Simplifying Synchronization with Locks
Problem
Solution
Discussion
See Also
16.7 Simplifying Producer/Consumer with the Queue Interface
Problem
Solution
Discussion
16.8 Optimizing Parallel Processing with Fork/Join
Problem
Solution
Discussion
16.9 Scheduling Tasks: Future Times, Background Saving in an Editor
Problem
Solution
Discussion
See Also
Chapter 17. Reflection, or “A Class Named Class”
17.0 Introduction
17.1 Getting a Class Descriptor
Problem
Solution
17.2 Finding and Using Methods and Fields
Problem
Solution
Discussion
17.3 Accessing Private Methods and Fields via Reflection
Problem
Solution
Discussion
17.4 Loading and Instantiating a Class Dynamically
Problem
Solution
Discussion
17.5 Constructing a Class from Scratch with a ClassLoader
Problem
Solution
Discussion
17.6 Constructing a Class from Scratch with JavaCompiler
Problem
Solution
Discussion
17.7 Performance Timing
Problem
Solution
Discussion
See Also
17.8 Printing Class Information
Problem
Solution
Discussion
17.9 Listing Classes in a Package
Problem
Solution
Discussion
17.10 Using and Defining Annotations
Problem
Solution
Discussion
17.11 Finding Plug-In-Like Classes via Annotations
Problem
Solution
Discussion
See Also
17.12 Program: CrossRef
See Also
Chapter 18. Using Java with Other Languages
18.0 Introduction
18.1 Running an External Program from Java
Problem
Solution
Discussion
18.2 Running a Program and Capturing Its Output
Problem
Solution
Discussion
See Also
18.3 Calling Other Languages via javax.script
Problem
Solution
Discussion
See Also
18.4 Mixing Languages with GraalVM
Problem
Solution
Discussion
18.5 Marrying Java and Perl
Problem
Solution
Discussion
See Also
18.6 Calling Other Languages via Native Code
Problem
Solution
Discussion
See Also
18.7 Calling Java from Native Code
Problem
Solution
Discussion
Afterword
Appendix A. Java Then and Now
Introduction: Always in Motion the Java Is
What Was New in Java 8
Java 8 Language Changes
Java 8 API Changes
What Was New in Java 9
Java 9 Language Changes
Java 9 API Changes
What Was New in Java 10 (March 2018)
Java 10 Language Changes
Java 10 API Changes
See Also
What Was New in Java 11 (September 2018)
Java 11 API Changes
What Was New in Java 12 (March 2019)
Java 12 Language Changes
Java 12 API Changes
What Is New in Java 13 (September 2019)
Looking Ahead
Index
About the Author