Java Reference
In-Depth Information
Set - This interface defines methods available to any collection without any defined sequence or ordering of the
contained elements.
List - This interfaces defines behavior for collections which use a numeric index to define an element's
position.
SortedSet - This interface is used to describe collections that use 'natural ordering” to organize elements. The
elements of a SortedSet must implement the interface Comparable , which defines a compareTo method. The
compareTo method returns a numeric result used to organize the objects within the SortedSet .
Map has a subinterface, as well—the SortedMap . Like SortedSet , this interface is used for maps with natural
ordering. The collections that implement this interface must have a way to compare keys of the elements that will
indicate whether one is greater than, equal to or less than the other. The keys must implement the Comparable
interface.
Pattern Use
A number of patterns figure heavily in the Collections Framework. In the general framework, there is strong use
of the Prototype and Iterator patterns. The Collections class also has a pattern associated with it—the Decorator.
Prototype (see page 28): The Prototype pattern uses one object as a template or basis for the creation of a new
object. Given the purpose of the collection classes, it's not surprising that they all support a copy operation clone.
That copy operation returns a new copy of the current collections. All of the collection classes implement the
Cloneable interface and provide a shallow copy when their clone method is called. In this case, a shallow copy
means that a new collection instance is returned, but all of the internally stored elements are the same objects as
those stored in the original collection.
Iterator (see page 69): All Collection implementors give you the ability to retrieve an object to easily (and
generically) cycle through the elements of the collection. The Iterator pattern, too, enables simpler cycling
through the elements of a collection. The Collection interface defines a method called iterator , and the List
interface has a listIterator method. These methods return interface implementors that allow users to move
through a collection. The Iterator interface is for forward-only navigation, and the ListIterator provides
both forward and backward movement within a collection. The names are a not-so-subtle giveaway. Actually,
both interfaces fall a bit short of the classic Iterator pattern, since they don't define all of the core
methods—specifically, neither interface provides an explicit first method. The goal is to abstract navigational
functionality from the underlying collection implementation, though. So, the central intent of these interfaces is
the same as for the Iterator pattern.
Collection classes use inner classes to provide concrete Iterators. When you call iterator or listIterator , the
collection creates an inner class object and returns it to the caller.
Decorator (see page 166): The Collections class uses the Decorator pattern to extend the functionality of
collections by providing objects that modify the behavior of the collections without changing the collections. The
class has three groups of methods that generate classes with additional capabilities. Table 6-2 shows the groups of
methods and what they produce:
Table 6-2. Method names and functionality
Begins with
Resulting functionality
singleton
Produces an immutable, one-element collection
synchronized
Produces a collection with synchronized methods
unmodifiable
Produces an immutable collection
Calling any of the methods from these groups produces an object that enhances the capabilities of the collection
that you pass in and adds to what it can do.
The Collections class actually has a set of inner classes that it uses to provide these added capabilities. So,
calling synchronizedCollection will generate a wrapper object around the inner collection which will ensure
that methods belonging to the Collection interface will be synchronized.
Note:
 
Search WWH ::




Custom Search