You are previewing Beginning Java 8 Language Features: Lambda Expressions, Inner Classes, Th reads, I/O, Collections,and Streams.
O'Reilly logo
Beginning Java 8 Language Features: Lambda Expressions, Inner Classes, Th reads, I/O, Collections,and Streams

Book Description

Beginning Java 8 Language Features covers essential and advanced features of the Java programming language such as the new lambda expressions (closures), inner classes, threads, I/O, Collections, garbage collection, streams, and more. Author Kishori Sharan provides over 60 diagrams and 290 complete programs to help you visualize and better understand the topics covered in this book.

The book starts with a series of chapters on the essential language features provided by Java, including annotations, inner classes, reflection, and generics. These topics are then complemented by details of how to use lambda expressions, allowing you to build powerful and efficient Java programs. The chapter on threads follows this up and discusses everything from the very basic concepts of a thread to the most advanced topics such as synchronizers, the fork/join framework, and atomic variables.

This book contains unmatched coverage of Java I/O, including NIO 2.0, the Path API, the FileVisitor API, the watch service and asynchronous file I/O. With this in-depth knowledge, your data- and file-management programs will be able to take advantage of every feature of Java's powerful I/O framework.

Finally, you'll learn how to use the Stream API, a new, exciting addition to Java 8, to perform aggregate operations on collections of data elements using functional-style programming. You'll examine the details of stream processing such as creating streams from different data sources, learning the difference between sequential and parallel streams, applying the filter-map-reduce pattern, and dealing with optional values.

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Dedication
  5. Contents at a Glance
  6. Contents
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Foreword
  11. Introduction
  12. Chapter 1: Annotations
    1. What Are Annotations?
    2. Declaring an Annotation Type
    3. Restrictions on Annotation Types
      1. Restriction #1
      2. Restriction #2
      3. Restriction #3
      4. Restriction #4
      5. Restriction #5
      6. Restriction #6
    4. Default Value of an Annotation Element
    5. Annotation Type and Its Instances
    6. Using Annotations
      1. Primitive Types
      2. String Types
      3. Class Types
      4. Enum Type
      5. Annotation Type
      6. Array Type Annotation Element
    7. No Null Value in an Annotation
    8. Shorthand Annotation Syntax
    9. Marker Annotation Types
    10. Meta-Annotation Types
      1. The Target Annotation Type
      2. The Retention Annotation
      3. The Inherited Annotation Type
      4. The Documented Annotation
      5. The Repeatable Annotation
      6. The Native Annotation
    11. Commonly Used Standard Annotations
      1. The Deprecated Annotation Type
      2. The Override Annotation Type
      3. The SuppressWarnings Annotation Type
      4. The FunctionalInterface Annotation Type
    12. Annotating a Java Package
    13. Accessing Annotations at Runtime
    14. Evolving Annotation Types
    15. Annotation Processing at Source Code Level
    16. Summary
  13. Chapter 2: Inner Classes
    1. What Is an Inner Class?
    2. Advantages of Using Inner Classes
    3. Types of Inner Classes
      1. Member Inner Class
      2. Local Inner Class
      3. Anonymous Inner Class
    4. A static Member Class Is Not an Inner Class
    5. Creating Objects of Inner Classes
    6. Accessing Enclosing Class Members
    7. Restrictions on Accessing Local Variables
    8. Inner Class and Inheritance
    9. No static Members in an Inner Class
    10. Generated Class Files for Inner Classes
    11. Inner Classes and the Compiler Magic
    12. Closures and Callbacks
    13. Defining Inner Classes in Static Contexts
    14. Summary
  14. Chapter 3: Reflection
    1. What Is Reflection?
    2. Reflection in Java
    3. The java.lang.Class Class
    4. Reflecting on a Class
    5. Reflecting on Fields
    6. Reflecting on an Executable
      1. Reflecting on Methods
      2. Reflecting on Constructors
    7. Creating Objects
    8. Invoking Methods
    9. Accessing Fields
    10. Bypassing Accessibility Check
    11. Reflecting on Arrays
    12. Expanding an Array
    13. Who Should Use Reflection?
    14. Summary
  15. Chapter 4: Generics
    1. What Are Generics?
    2. Supertype-Subtype Relationship
    3. Raw Type
    4. Unbounded Wildcards
    5. Upper-Bounded Wildcards
    6. Lower-Bounded Wildcards
    7. Generic Methods and Constructors
    8. Type Inference in Generic Object Creation
    9. No Generic Exception Classes
    10. No Generic Anonymous Classes
    11. Generics and Arrays
    12. Runtime Class Type of Generic Objects
    13. Heap Pollution
    14. Varargs Methods and Heap Pollution Warnings
    15. Summary
  16. Chapter 5: Lambda Expressions
    1. What Is a Lambda Expression?
    2. Why Do We Need Lambda Expressions?
    3. Syntax for Lambda Expressions
      1. Omitting Parameter Types
      2. Declaring a Single Parameter
      3. Declaring No Parameters
      4. Parameters with Modifiers
      5. Declaring Body of Lambda Expressions
    4. Target Typing
    5. Functional Interfaces
      1. Using the @FunctionalInterface Annotation
      2. Generic Functional Interface
      3. Intersection Type and Lambda Expressions
      4. Commonly Used Functional Interfaces
      5. Using the Function<T,R> Interface
      6. Using the Predicate<T> Interface
      7. Using Functional Interfaces
    6. Method References
      1. Static Method References
      2. Instance Method References
      3. Supertype Instance Method References
      4. Constructor references
      5. Generic Method References
    7. Lexical Scoping
    8. Variable Capture
    9. Jumps and Exits
    10. Recursive Lambda Expressions
    11. Comparing Objects
    12. Summary
  17. Chapter 6: Threads
    1. What Is a Thread?
    2. Creating a Thread in Java
    3. Specifying Your Code for a Thread
      1. Inheriting Your Class from the Thread Class
      2. Implementing the Runnable Interface
      3. Using a Method Reference
      4. A Quick Example
    4. Using Multiple Threads in a Program
    5. Issues in Using Multiple Threads
    6. Java Memory Model (JMM)
      1. Atomicity
      2. Visibility
      3. Ordering
    7. Object’s Monitor and Threads Synchronization
    8. The Producer/Consumer Synchronization Problem
    9. Which Thread Is Executing?
    10. Letting a Thread Sleep
    11. I will Join You in Heaven
    12. Be Considerate to Others and Yield
    13. Life Cycle of a Thread
    14. Priority of a Thread
    15. Is It a Demon or a Daemon?
    16. Am I Interrupted?
    17. Threads Work in a Group
    18. Volatile Variables
    19. Stopping, Suspending, and Resuming a Thread
    20. Handling an Uncaught Exception in a Thread
    21. New Thread Concurrency Packages
    22. Atomic Variables
      1. Scalar Atomic Variable Classes
      2. Atomic Arrays Classes
      3. Atomic Field Updater Classes
      4. Atomic Compound Variable Classes
    23. Explicit Locks
    24. Synchronizers
      1. Semaphores
      2. Barriers
      3. Phasers
      4. Latches
      5. Exchangers
    25. The Executor Framework
      1. Result-Bearing Tasks
      2. Scheduling a Task
      3. Handling Uncaught Exceptions in a Task Execution
      4. Executor’s Completion Service
    26. The Fork/Join Framework
      1. Steps in Using the Fork/Join Framework
      2. A Fork/Join Example
    27. Thread-Local Variables
    28. Setting Stack Size of a Thread
    29. Summary
  18. Chapter 7: Input/Output
    1. What Is Input/Output?
    2. Working with Files
      1. Creating a File Object
      2. Knowing the Current Working Directory
      3. Checking for a File’s Existence
      4. Which Path Do You Want to Go?
      5. Creating, Deleting, and Renaming Files
      6. Working with File Attributes
      7. Copying a File
      8. Knowing the Size of a File
      9. Listing All Files and Directories
    3. The Decorator Pattern
    4. Input/Output Streams
      1. Reading from File Using an Input Stream
      2. Writing Data to a File Using an Output Stream
    5. Input Stream Meets the Decorator Pattern
      1. BufferedInputStream
      2. PushbackInputStream
    6. Output Stream Meets the Decorator Pattern
      1. PrintStream
    7. Using Pipes
    8. Reading and Writing Primitive Data Types
    9. Object Serialization
      1. Serializing Objects
      2. Deserializing Objects
      3. Externalizable Object Serialization
    10. Serialization of transient Fields
    11. Advanced Object Serialization
      1. Writing an Object More Than Once to a Stream
      2. Class Evolution and Object Serialization
      3. Stopping Serialization
    12. Readers and Writers
    13. Custom Input/Output Streams
    14. Random Access Files
    15. Copying the Contents of a File
    16. Standard Input/Output/Error Streams
    17. Console and Scanner Classes
    18. StringTokenizer and StreamTokenizer
    19. Summary
  19. Chapter 8: Working with Archive Files
    1. What Is an Archive File?
    2. Data Compression
    3. Checksum
    4. Compressing Byte Arrays
    5. Working with ZIP File Format
    6. Working with GZIP File Format
    7. Working with JAR File Format
      1. Creating a JAR File
      2. Updating a JAR File
      3. Indexing a JAR File
      4. Extracting an Entry from a JAR File
      5. Listing the Contents of a JAR File
      6. The Manifest File
      7. Sealing a Package in a JAR File
    8. Using the JAR API
    9. Accessing Resources from a JAR File
    10. Summary
  20. Chapter 9: New Input/Output
    1. What Is NIO?
    2. Buffers
    3. Reading from and Writing to a Buffer
    4. Read-Only Buffers
    5. Different Views of a Buffer
    6. Character Set
    7. Channels
    8. Reading/Writing Files
    9. Memory-Mapped File I/O
    10. File Locking
    11. Copying Contents of a File
    12. Knowing the Byte Order of a Machine
    13. Byte Buffer and Its Byte Order
    14. Summary
  21. Chapter 10: New Input/Output 2
    1. What Is New Input/Output 2?
    2. Working with a File System
    3. Working with Paths
      1. Creating a Path Object
      2. Accessing Components of a Path
      3. Comparing Paths
      4. Normalizing, Resolving, and Relativizing Paths
    4. Symbolic Links
    5. Different Forms of a Path
    6. Performing File Operations on a Path
      1. Creating New Files
      2. Deleting Files
      3. Checking for Existence of a File
      4. Copying and Moving Files
      5. Commonly Used File Attributes
      6. Probing the Content Type of a File
      7. Reading the Contents of a File
      8. Writing to a File
      9. Random Access to a File
    7. Traversing a File Tree
    8. Matching Paths
    9. Managing File Attributes
      1. Checking for a File Attribute View Support
      2. Reading and Updating File Attributes
      3. Managing the Owner of a File
      4. Managing ACL File Permissions
      5. Managing POSIX File Permissions
    10. Watching a Directory for Modifications
      1. Create a Watch Service
      2. Register the Directory with the Watch Service
      3. Retrieve a WatchKey from the Watch Service Queue
      4. Process the Events
      5. Reset the WatchKey after Processing Events
      6. Close the Watch Service
    11. Asynchronous File I/O
    12. Summary
  22. Chapter 11: Garbage Collection
    1. What Is Garbage Collection?
    2. Memory Allocation in Java
    3. Garbage Collection in Java
    4. Invoking the Garbage Collector
    5. Object Finalization
    6. Finally or Finalize?
    7. Object Resurrection
    8. State of an Object
    9. Weak References
    10. Accessing and Clearing a Referent’s Reference
    11. Using the SoftReference Class
    12. Using the ReferenceQueue Class
    13. Using the WeakReference Class
    14. Using the PhantomReference Class
    15. Summary
  23. Chapter 12: Collections
    1. What Is a Collection?
    2. Need for a Collection Framework
    3. Architecture of the Collection Framework
    4. The Collection Interface
      1. Methods for Basic Operations
      2. Methods for Bulk (or Group) Operations
      3. Methods for Aggregate Operations
      4. Methods for Array Operations
      5. Methods for Comparison Operations
    5. A Quick Example
    6. Traversing Collections
      1. Using an Iterator
      2. Using a for-each Loop
      3. Using the forEach() Method
    7. Using Different Types of Collections
      1. Working with Sets
      2. Working with Lists
      3. Working with Queues
      4. Working with Maps
    8. Applying Algorithms to Collections
      1. Sorting a List
      2. Searching a List
      3. Shuffling, Reversing, Swapping, and Rotating a List
    9. Creating Different Views of a Collection
      1. Read-Only Views of Collections
      2. Synchronized View of a Collection
      3. Checked Collections
    10. Creating Empty Collections
    11. Creating Singleton Collections
    12. Understanding Hash-based Collections
    13. Summary
  24. Chapter 13: Streams
    1. What Is a Stream?
      1. Streams Have No Storage
      2. Infinite Streams
      3. Internal Iteration vs. External Iteration
      4. Imperative vs. Functional
      5. Stream Operations
      6. Ordered Streams
      7. Streams Are Not Reusable
      8. Architecture of the Streams API
    2. A Quick Example
    3. Creating Streams
      1. Streams from Values
      2. Empty Streams
      3. Streams from Functions
      4. Streams from Arrays
      5. Streams from Collections
      6. Streams from Files
      7. Streams from Other Sources
    4. Representing an Optional Value
    5. Applying Operations on Streams
      1. Debugging a Stream Pipeline
      2. Applying the ForEach Operation
      3. Applying the Map Operation
      4. Flattening Streams
      5. Applying the Filter Operation
      6. Applying the Reduce Operation
    6. Collecting Data Using Collectors
    7. Collecting Summary Statistics
    8. Collecting Data in Maps
    9. Joining Strings Using Collectors
    10. Grouping Data
    11. Partitioning Data
    12. Adapting the Collector Results
    13. Finding and Matching in Streams
    14. Parallel Streams
    15. Summary
  25. Index