Skip to Content
Data Structures and the Java Collections Framework, Third Edition
book

Data Structures and the Java Collections Framework, Third Edition

by William J. Collins
January 2011
Intermediate to advanced
760 pages
20h 16m
English
Wiley
Content preview from Data Structures and the Java Collections Framework, Third Edition

APPENDIX 1

Additional Features of the JAVA Collections Framework

A1.1 Introduction

The Java Collections Framework has several features beyond those encountered so far in this book. This appendix focuses on two of those features: serialization and fail-fast iterators.

A1.2 Serialization

Suppose we have gone to the trouble of creating a large and complex HashMap object as part of a project. After we have used that HashMap object in an execution of the project, we might want to save the HashMap, on file, so that we can later resume the execution of the project without having to re-construct the HashMap. Fortunately, this is easily done with just a couple of statements.

How? All of the collection classes in the Java Collections Framework implement the Serializable interface that is in the package java.io. This interface has no methods, but merely provides information to the Java virtual machine about sending instances of the class to/from a stream (a sequence of bytes). Specifically, any class that implements the Serializable interface will be able to copy any object in the class to an output stream—that is, to "serialize" the elements in the object. "Deserialization" reconstructs the original object from an input stream.

For a simple example, suppose we have created an ArrayList object named fruits, whose elements are of type string. We can create an Objectoutputstream and then write fruits to the FileOutputstream object whose path is "fruits.ser" as follows:

try { ObjectOutputStream ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beginning Java Data Structures and Algorithms

Beginning Java Data Structures and Algorithms

James Cutajar

Publisher Resources

ISBN: 9780470482674Purchase book