Core Java® Volume II—Advanced Features Tenth Edition This Page Intentionally Left Blank Core Java® Volume II—Advanced Features Tenth Edition
Total Page:16
File Type:pdf, Size:1020Kb
Core Java® Volume II—Advanced Features Tenth Edition This page intentionally left blank Core Java® Volume II—Advanced Features Tenth Edition Cay S. Horstmann Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi • Mexico City São Paulo • Sydney • Hong Kong • Seoul • Singapore • Taipei • Tokyo The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419. For government sales inquiries, please contact [email protected]. For questions about sales outside the United States, please contact [email protected]. Visit us on the Web: informit.com/ph Library of Congress Catalog Number: 2016952666 Copyright © 2017 Oracle and/or its affiliates. All rights reserved. 500 Oracle Parkway, Redwood Shores, CA 94065 Portions © 2017 Cay S. Horstmann All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. Oracle America Inc. does not make any representations or warranties as to the accuracy, adequacy or completeness of any information contained in this work, and is not responsible for any errors or omissions. ISBN-13: 978-0-13-417729-8 ISBN-10: 0-13-417729-0 Text printed in the United States of America. 1 16 Contents Preface ........................................................................................................... xv Acknowledgments ......................................................................................... xix Chapter 1: The Java SE 8 Stream Library ..................................................... 1 1.1 From Iterating to Stream Operations ........................................................ 2 1.2 Stream Creation ............................................................................................ 5 1.3 The filter , map , and flatMap Methods ............................................................. 9 1.4 Extracting Substreams and Concatenating Streams ............................. 10 1.5 Other Stream Transformations ................................................................ 11 1.6 Simple Reductions ..................................................................................... 12 1.7 The Optional Type ..................................................................................... 13 1.7.1 How to Work with Optional Values .......................................... 14 1.7.2 How Not to Work with Optional Values .................................. 15 1.7.3 Creating Optional Values ........................................................... 16 1.7.4 Composing Optional Value Functions with flatMap .................. 16 1.8 Collecting Results ...................................................................................... 19 1.9 Collecting into Maps ................................................................................. 24 1.10 Grouping and Partitioning ....................................................................... 28 1.11 Downstream Collectors ............................................................................ 29 1.12 Reduction Operations ............................................................................... 33 1.13 Primitive Type Streams ............................................................................. 36 1.14 Parallel Streams .......................................................................................... 41 Chapter 2: Input and Output ........................................................................ 47 2.1 Input/Output Streams .............................................................................. 48 2.1.1 Reading and Writing Bytes ........................................................ 48 2.1.2 The Complete Stream Zoo ......................................................... 51 2.1.3 Combining Input/Output Stream Filters ................................ 55 v vi Contents 2.2 Text Input and Output .............................................................................. 60 2.2.1 How to Write Text Output ......................................................... 60 2.2.2 How to Read Text Input ............................................................. 62 2.2.3 Saving Objects in Text Format ................................................... 63 2.2.4 Character Encodings ................................................................... 67 2.3 Reading and Writing Binary Data ........................................................... 69 2.3.1 The DataInput and DataOutput interfaces .......................................... 69 2.3.2 Random-Access Files .................................................................. 72 2.3.3 ZIP Archives ................................................................................. 77 2.4 Object Input/Output Streams and Serialization ................................... 80 2.4.1 Saving and Loading Serializable Objects ................................. 80 2.4.2 Understanding the Object Serialization File Format .............. 85 2.4.3 Modifying the Default Serialization Mechanism .................... 92 2.4.4 Serializing Singletons and Typesafe Enumerations ............... 94 2.4.5 Versioning ..................................................................................... 95 2.4.6 Using Serialization for Cloning ................................................. 98 2.5 Working with Files .................................................................................. 100 2.5.1 Paths ............................................................................................ 101 2.5.2 Reading and Writing Files ........................................................ 104 2.5.3 Creating Files and Directories ................................................. 105 2.5.4 Copying, Moving, and Deleting Files .................................... 106 2.5.5 Getting File Information ........................................................... 108 2.5.6 Visiting Directory Entries ......................................................... 110 2.5.7 Using Directory Streams .......................................................... 111 2.5.8 ZIP File Systems ........................................................................ 115 2.6 Memory-Mapped Files ........................................................................... 116 2.6.1 Memory-Mapped File Performance ....................................... 116 2.6.2 The Buffer Data Structure ......................................................... 124 2.6.3 File Locking ................................................................................ 126 2.7 Regular Expressions ................................................................................ 128 Chapter 3: XML ............................................................................................ 143 3.1 Introducing XML ..................................................................................... 144 3.1.1 The Structure of an XML Document ....................................... 146 Contents vii 3.2 Parsing an XML Document .................................................................... 149 3.3 Validating XML Documents ................................................................... 162 3.3.1 Document Type Definitions ..................................................... 163 3.3.2 XML Schema .............................................................................. 172 3.3.3 A Practical Example .................................................................. 175 3.4 Locating Information with XPath .......................................................... 190 3.5 Using Namespaces ................................................................................... 196 3.6 Streaming Parsers ..................................................................................... 199 3.6.1 Using the SAX Parser ................................................................ 199 3.6.2 Using the StAX Parser ............................................................... 205 3.7 Generating XML Documents ................................................................. 208 3.7.1 Documents without Namespaces ........................................... 209 3.7.2 Documents with Namespaces ................................................. 209 3.7.3 Writing Documents ................................................................... 210 3.7.4 An Example: Generating an SVG File .................................... 211 3.7.5 Writing an XML Document