Book description
From lambda expressions and JavaFX 8 to new support for network programming and mobile development, Java 8 brings a wealth of changes. This cookbook helps you 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 debugging and data structures to GUI development and functional programming.
Table of contents
- Dedication
- Preface
-
1. Getting Started: Compiling, Running, and Debugging
- Introduction
- Compiling and Running Java: JDK
- Editing and Compiling with a Syntax-Highlighting Editor
- Compiling, Running, and Testing with an IDE
- Using CLASSPATH Effectively
- Downloading and Using the Code Examples
- Automating Compilation with Apache Ant
- Automating Dependencies, Compilation, Testing, and Deployment with Apache Maven
- Automating Dependencies, Compilation, Testing, and Deployment with Gradle
- Dealing with Deprecation Warnings
- Conditional Debugging Without #ifdef
- Maintaining Program Correctness with Assertions
- Debugging with JDB
- Avoiding the Need for Debuggers with Unit Testing
- Maintaining Your Code with Continuous Integration
- Getting Readable Tracebacks
- Finding More Java Source Code: Programs, Frameworks, Libraries
- 2. Interacting with the Environment
-
3. Strings and Things
- Introduction
- Taking Strings Apart with Substrings
- Breaking Strings Into Words
- Putting Strings Together with StringBuilder
- Processing a String One Character at a Time
- Aligning Strings
- Converting Between Unicode Characters and Strings
- Reversing a String by Word or by Character
- Expanding and Compressing Tabs
- Controlling Case
- Indenting Text Documents
- Entering Nonprintable Characters
- Trimming Blanks from the End of a String
- Parsing Comma-Separated Data
- Program: A Simple Text Formatter
- Program: Soundex Name Comparisons
-
4. Pattern Matching with Regular Expressions
- Introduction
- Regular Expression Syntax
- Using regexes in Java: Test for a Pattern
- Finding the Matching Text
- Replacing the Matched Text
- Printing All Occurrences of a Pattern
- Printing Lines Containing a Pattern
- Controlling Case in Regular Expressions
- Matching “Accented” or Composite Characters
- Matching Newlines in Text
- Program: Apache Logfile Parsing
- Program: Data Mining
- Program: Full Grep
-
5. Numbers
- Introduction
- Checking Whether a String Is a Valid Number
- Storing a Larger Number in a Smaller Number
- Converting Numbers to Objects and Vice Versa
- Taking a Fraction of an Integer Without Using Floating Point
- Ensuring the Accuracy of Floating-Point Numbers
- Comparing Floating-Point Numbers
- Rounding Floating-Point Numbers
- Formatting Numbers
- Converting Between Binary, Octal, Decimal, and Hexadecimal
- Operating on a Series of Integers
- Working with Roman Numerals
- Formatting with Correct Plurals
- Generating Random Numbers
- Calculating Trigonometric Functions
- Taking Logarithms
- Multiplying Matrices
- Using Complex Numbers
- Handling Very Large Numbers
- Program: TempConverter
- Program: Number Palindromes
- 6. Dates and Times—New API
-
7. Structuring Data with Java
- Introduction
- Using Arrays for Data Structuring
- Resizing an Array
- The Collections Framework
- Like an Array, but More Dynamic
- Using Generic Collections
- Avoid Casting by Using Generics
- How Shall I Iterate Thee? Let Me Enumerate the Ways
- Eschewing Duplicates with a Set
- Using Iterators or Enumerations for Data-Independent Access
- Structuring Data in a Linked List
- Mapping with Hashtable and HashMap
- Storing Strings in Properties and Preferences
- Sorting a Collection
- Avoiding the Urge to Sort
- Finding an Object in a Collection
- Converting a Collection to an Array
- Rolling Your Own Iterator
- Stack
- Multidimensional Structures
- Program: Timing Comparisons
-
8. Object-Oriented Techniques
- Introduction
- Formatting Objects for Printing with toString()
- Overriding the equals() and hashCode() Methods
- Using Shutdown Hooks for Application Cleanup
- Using Inner Classes
- Providing Callbacks via Interfaces
- Polymorphism/Abstract Methods
- Passing Values
- Using Typesafe Enumerations
- Enforcing the Singleton Pattern
- Roll Your Own Exceptions
- Using Dependency Injection
- Program: Plotter
-
9. Functional Programming Techniques: Functional Interfaces, Streams, Parallel Collections
- Introduction
- Using Lambdas/Closures Instead of Inner Classes
- Using Lambda Predefined Interfaces Instead of Your Own
- Simplifying Processing with Streams
- Improving Throughput with Parallel Streams and Collections
- Creating Your Own Functional Interfaces
- Using Existing Code as Functional with Method References
- Java Mixins: Mixing in Methods
-
10. Input and Output
- Introduction
- Reading Standard Input
- Reading from the Console or Controlling Terminal; Reading Passwords Without Echoing
- Writing Standard Output or Standard Error
- Printing with Formatter and printf
- Scanning Input with StreamTokenizer
- Scanning Input with the Scanner Class
- Scanning Input with Grammatical Structure
- Opening a File by Name
- Copying a File
- Reading a File into a String
- Reassigning the Standard Streams
- Duplicating a Stream as It Is Written
- Reading/Writing a Different Character Set
- Those Pesky End-of-Line Characters
- Beware Platform-Dependent File Code
- Reading “Continued” Lines
- Reading/Writing Binary Data
- Seeking to a Position within a File
- Writing Data Streams from C
- Saving and Restoring Java Objects
- Preventing ClassCastExceptions with SerialVersionUID
- Reading and Writing JAR or ZIP Archives
- Finding Files in a Filesystem-Neutral Way with getResource() and getResourceAsStream()
- Reading and Writing Compressed Files
- Learning about the Communications API for Serial and Parallel Ports
- Save User Data to Disk
- Program: Text to PostScript
-
11. Directory and Filesystem Operations
- Introduction
- Getting File Information
- Creating a File
- Renaming a File
- Deleting a File
- Creating a Transient File
- Changing File Attributes
- Listing a Directory
- Getting the Directory Roots
- Creating New Directories
- Using Path instead of File
- Using the FileWatcher Service to Get Notified about File Changes
- Program: Find
-
12. Media: Graphics, Audio, Video
- Introduction
- Painting with a Graphics Object
- Showing Graphical Components Without Writing Main
- Drawing Text
- Drawing Centered Text in a Component
- Drawing a Drop Shadow
- Drawing Text with 2D
- Drawing Text with an Application Font
- Drawing an Image
- Reading and Writing Images with javax.imageio
- Playing an Audio/Sound File
- Playing a Video File
- Printing in Java
- Program: PlotterAWT
- Program: Grapher
-
13. Network Clients
- Introduction
- Contacting a Server
- Finding and Reporting Network Addresses
- Handling Network Errors
- Reading and Writing Textual Data
- Reading and Writing Binary Data
- Reading and Writing Serialized Data
- UDP Datagrams
- Program: TFTP UDP Client
- URI, URL, or URN?
- REST Web Service Client
- SOAP Web Service Client
- Program: Telnet Client
- Program: Chat Client
- Program: Simple HTTP Link Checker
-
14. Graphical User Interfaces
- Introduction
- Displaying GUI Components
- Run Your GUI on the Event Dispatching Thread
- Designing a Window Layout
- A Tabbed View of Life
- Action Handling: Making Buttons Work
- Action Handling Using Anonymous Inner Classes
- Action Handling Using Lambdas
- Terminating a Program with “Window Close”
- Dialogs: When Later Just Won’t Do
- Catching and Formatting GUI Exceptions
- Getting Program Output into a Window
- Choosing a Value with JSpinner
- Choosing a File with JFileChooser
- Choosing a Color
- Formatting JComponents with HTML
- Centering a Main Window
- Changing a Swing Program’s Look and Feel
- Enhancing Your Swing GUI for Mac OS X
- Building Your GUI Application with JavaFX
- Program: Custom Font Chooser
- Program: Custom AWT/Swing Layout Manager
-
15. Internationalization and Localization
- Introduction
- Creating a Button with I18N Resources
- Listing Available Locales
- Creating a Menu with I18N Resources
- Writing Internationalization Convenience Routines
- Creating a Dialog with I18N Resources
- Creating a Resource Bundle
- Extracting Strings from Your Code
- Using a Particular Locale
- Setting the Default Locale
- Formatting Messages with MessageFormat
- Program: MenuIntl
- Program: BusCard
-
16. Server-Side Java
- Introduction
- Opening a Server Socket for Business
- Returning a Response (String or Binary)
- Returning Object Information Across a Network Connection
- Handling Multiple Clients
- Serving the HTTP Protocol
- Securing a Web Server with SSL and JSSE
- Network Logging
- Network Logging with SLF4J
- Network Logging with log4j
- Network Logging with java.util.logging
- Finding Network Interfaces
- Program: A Java Chat Server
- 17. Java and Electronic Mail
-
18. Database Access
- Introduction
- Easy Database Access with JPA and/or Hibernate
- JDBC Setup and Connection
- Connecting to a JDBC Database
- Sending a JDBC Query and Getting Results
- Using JDBC Prepared Statements
- Using Stored Procedures with JDBC
- Changing Data Using a ResultSet
- Storing Results in a RowSet
- Changing Data Using SQL
- Finding JDBC Metadata
- Program: SQLRunner
- 19. Processing JSON Data
-
20. Processing XML
- Introduction
- Converting Between Objects and XML with JAXB
- Converting Between Objects and XML with Serializers
- Transforming XML with XSLT
- Parsing XML with SAX
- Parsing XML with DOM
- Finding XML Elements with XPath
- Verifying Structure with Schema or DTD
- Generating Your Own XML with DOM and the XML Transformer
- Program: xml2mif
-
21. Packages and Packaging
- Introduction
- Creating a Package
- Documenting Classes with Javadoc
- Beyond Javadoc: Annotations/Metadata
- Archiving with jar
- Running a Program from a JAR
- Preparing a Class as a JavaBean
- Pickling Your Bean into a JAR
- Packaging a Servlet into a WAR File
- “Write Once, Install Anywhere”
- “Write Once, Install on Mac OS X”
- Java Web Start
- Signing Your JAR File
-
22. Threaded Java
- Introduction
- Running Code in a Different Thread
- Displaying a Moving Image with Animation
- Stopping a Thread
- Rendezvous and Timeouts
- Synchronizing Threads with the synchronized Keyword
- Simplifying Synchronization with Locks
- Synchronizing Threads the Hard Way with wait( ) and notifyAll( )
- Simplifying Producer/Consumer with the Queue Interface
- Optimizing Parallel Processing with Fork/Join
- Background Saving in an Editor
- Program: Threaded Network Server
- Simplifying Servers Using the Concurrency Utilities
-
23. Reflection, or “A Class Named Class”
- Introduction
- Getting a Class Descriptor
- Finding and Using Methods and Fields
- Accessing Private Methods and Fields via Reflection
- Loading and Instantiating a Class Dynamically
- Constructing a Class from Scratch with a ClassLoader
- Performance Timing
- Printing Class Information
- Listing Classes in a Package
- Using and Defining Annotations
- Finding Plug-in-like Classes via Annotations
- Program: CrossRef
- Program: AppletViewer
- 24. Using Java with Other Languages
- Afterword
-
A. Java Then and Now
- Introduction: Always in Motion the Java Is
- Java Preview: HotJava
- Java Arrives: 1.0
- What Was New in Java 1.1
- What Was New in Java 2 (Java SDK 1.2)
- What Was New in Java 1.3
- What Was New in Java 1.4
- What Was New in Java 5
- What Was New in Java 6
- What Was New in Java 7
- What Is New in Java 8
- Look Away Beyond the Blue Horizon…
- Index
- Colophon
- Copyright
Product information
- Title: Java Cookbook, 3rd Edition
- Author(s):
- Release date: July 2014
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449337049
You might also like
book
Java Cookbook, 4th Edition
Java continues to grow and evolve, and this cookbook continues to evolve in tandem. With this …
book
Java in a Nutshell, 6th Edition
The latest edition of Java in a Nutshell is designed to help experienced Java programmers get …
book
Modern Java Recipes
The introduction of functional programming concepts in Java SE 8 was a drastic change for this …
book
Core Java for the Impatient, 3rd Edition
Clear, Concise Guide to the Core Language and Libraries--Updated through Java 17 Modern Java introduces major …