Java Reference
In-Depth Information
Software Engineering Observation 16.1
Collection is used commonly as a parameter type in methods to allow polymorphic
processing of all objects that implement interface Collection .
Software Engineering Observation 16.2
Most collection implementations provide a constructor that takes a Collection
argument, thereby allowing a new collection to be constructed containing the elements of
the specified collection.
Class Collections provides static methods that search , sort and perform other oper-
ations on collections. Section 16.7 discusses more about Collections methods. We also
cover Collections ' wrapper methods that enable you to treat a collection as a synchro-
nized collection (Section 16.13) or an unmodifiable collection (Section 16.14). Synchro-
nized collections are for use with multithreading (discussed in Chapter 23), which enables
programs to perform operations in parallel . When two or more threads of a program share
a collection, problems might occur. As an analogy, consider a traffic intersection. If all cars
were allowed to access the intersection at the same time, collisions might occur. For this
reason, traffic lights are provided to control access to the intersection. Similarly, we can
synchronize access to a collection to ensure that only one thread manipulates the collection
at a time. The synchronization wrapper methods of class Collections return synchro-
nized versions of collections that can be shared among threads in a program. Unmodifiable
collections are useful when clients of a class need to view a collection's elements, but they
should not be allowed to modify the collection by adding and removing elements.
16.6 Lists
A List (sometimes called a sequence ) is an ordered Collection that can contain duplicate
elements. Like array indices, List indices are zero based (i.e., the first element's index is
zero). In addition to the methods inherited from Collection , List provides methods for
manipulating elements via their indices, manipulating a specified range of elements,
searching for elements and obtaining a ListIterator to access the elements.
Interface List is implemented by several classes, including ArrayList , LinkedList
and Vector . Autoboxing occurs when you add primitive-type values to objects of these
classes, because they store only references to objects. Classes ArrayList and Vector are
resizable-array implementations of List . Inserting an element between existing elements
of an ArrayList or Vector is an inefficient operation—all elements after the new one must
be moved out of the way, which could be an expensive operation in a collection with a
large number of elements. A LinkedList enables efficient insertion (or removal) of ele-
ments in the middle of a collection, but is much less efficient than an ArrayList for
jumping to a specific element in the collection. We discuss the architecture of linked lists
in Chapter 21.
ArrayList and Vector have nearly identical behaviors. Operations on Vector s are syn-
chronized by default, whereas those on ArrayList s are not. Also, class Vector is from Java
1.0, before the collections framework was added to Java. As such, Vector has some methods
that are not part of interface List and are not implemented in class ArrayList . For
example, Vector methods addElement and add both append an element to a Vector , but
only method add is specified in interface List and implemented by ArrayList . Unsynchro-
 
 
Search WWH ::




Custom Search