Important Library Classes - Java
Important Library Classes - Java
gramming tasks.
In this appendix we briefly summarize details of some classes and interfaces from the
most important packages of the Java 2 Platform API. A competent Java programmer
should be familiar with most of these. This appendix is only a summary, and it should be
read in conjunction with the full API documentation.
interface C o l l e c t i o n This interface provides the core set of methods for most of the
collection-based classes defined in the j a v a . u t i l package, such as
A r r a y L i s t , HashSet, and LinkedList. It defines signatures for the
add, clear, i t e r a t o r , remove, and s i z e methods.
interface I t e r a t o r I t e r a t o r defines a simple and consistent interface for iterating over
the contents of a collection. Its three methods are hasNext, next,
and remove.
interface L i s t List is an extension of the Collection interface, and provides a
means to impose a sequence on the selection. As such, many of its
methods take an index parameter: for instance, add, get, remove, and
set. Classes such as ArrayList and LinkedList implement List.
interface Map The Map interface offers an alternative to list-based collections by
supporting the idea of associating each object in a collection with a
key value. Objects are added and retrieved via
its put and get methods. Note that a Map does not return an
I t e r a t o r , but its keyset method returns a Set of the keys, and its
values method returns a C o l l e c t i o n of the objects in the map.
interface Set Set extends the C o l l e c t i o n interface with the intention of
mandating that a collection contains no duplicate elements. It is worth
pointing out that, because it is an interface, Set has no actual
implication to enforce this restriction. This means that Set is actually
provided as a marker interface to enable collection implementers to
indicate that their classes fulfill this particular restriction.
class A r r a y L i s t An implementation of the L i s t interface that uses an array to provide
efficient direct access via integer indices to the objects it stores. If
objects are added or removed from anywhere other than the last
position in the list, then following items have to be moved to make
space or close the gap. Key methods are add, get, i t e r a t o r ,
remove, and size.
class C o l l e c t i o n s C o l l e c t i o n s is a collecting point for static methods that are used to
manipulate collections. Key methods are binarySearch, f i l l , and
sort.
class HashMap HashMap is an implementation of the Map interface. Key methods are
get, put, remove, and size. Iteration over a HashMap is usually a
two-stage process: obtain the set of keys via its keySet method, and
then iterate over the keys.
class HashSet HashSet is a hash-based implementation of the Set interface. It is
closer in usage to a C o l l e c t i o n than to a HashMap. Key methods
are add, remove, and size.
class LinkedList LinkedList is an implementation of the L i s t interface that uses an
internal linked structure to store objects. Direct access to the ends of
the list is efficient, but access to individual objects via an index is less
efficient than with an A r r a y L i s t . On the other hand, adding objects
or removing them from within the list requires no shifting of existing
objects. Key methods are add, g e t F i r s t , getLast, i t e r a t o r ,
removeFirst, removeLast, and size.
452 Appendices
class FileReader The FileReader class is used to open an external file ready for
reading its contents as characters. A FileReader object is often
passed to the constructor of another reader class (such as a
Buff enedReader) rather than being used directly. Key methods are
close and read.
class F i l e W r i t e r The F i l e W r i t e r class is used to open an external file ready for
writing character-based data. Pairs of constructors determine whether
an existing file will be appended or its existing contents discarded. A
F i l e W r i t e r object is often passed to the constructor of another
writer class (such as a BufferedWriter) rather than being used
directly. Key methods are close, f l u s h , and w r i t e .
class IOException IOException is a checked exception class that is at the root of the
exception hierarchy of most input/output exceptions.
class URL The URL class represents a Uniform Resource Locator: in other words, it
provides a way to describe the location of something on the Internet. In
fact, it can also be used to describe the location of something on a local
file system. We have included it here because classes from the
j a v a . io and javax.swing packages often use URL objects. Key
methods are getContent, g e t F i l e , getHost, getPath, and
openStream.