Java Reference
In-Depth Information
Table 9-4 The Collections Framework
CORE COLLECTION
GENERAL-PURPOSE
INTERFACES
DESCRIPTION
IMPLEMENTATIONS PROVIDED
Collection
The root of the collection hierarchy.
No implementation
Set
A collection that cannot contain duplicates. Models the
HashSet, TreeSet
mathematical abstraction of a set. Inherits from Collection.
SortedSet
A Set in ascending element order. Inherits from Set.
No implementation
List
An ordered collection which can contain duplicates. Inherits
ArrayList, LinkedList
from Collection.
Map
Maps keys (unique identifier values) to at most one other value
HashMap, TreeMap
each; cannot contain duplicate keys. Not a true collection, so
it does not inherit from Collection. Included in the Collections
Framework because it contains operations which allow
manipulation as a collection.
SortedMap
A Map in ascending key order. Inherits from Map.
No implementation
The requirements for the Password class indicate that a collection of
previously encrypted password values be kept in a password history list. The
Collections Framework provides an appropriate means of storing and manipu-
lating such a collection with relative ease. The specific implementation for the
collection used within the Password class is independent of users of the class,
which means it can be changed later, if necessary, without affecting programs
dependent on the functionality of the Password class.
Creating an ArrayList
The requirement for the Password class to maintain a collection of password
values can be implemented in a variety of ways. It seems intuitive, however, that
because a list of passwords is needed, an implementation using the List collec-
tion would be most appropriate. The framework provides two general-purpose
implementations of List: LinkedList and ArrayList. ArrayList allows program-
mers to resize the length of the list, inserting and deleting list members. The
ability to resize the list of passwords is specified in the requirements document.
The capacity of the ArrayList is the size of the array used to store the array ele-
ments, which is always at least as large as the list size — that is, the number of
elements actually in the list. As elements are added, when the list size reaches the
capacity, the capacity is increased automatically. Additionally, ArrayList typically
yields faster access than LinkedList. The time to position to an element in an
ArrayList is the same for any element, but varies linearly for a LinkedList,
depending on the location of the element in the list. Because of the way the
password list will need to be manipulated, ArrayList is the most appropriate
implementation to use.
 
Search WWH ::




Custom Search