Java Reference
In-Depth Information
21.14. Writing Collection Implementations
You will usually find that at least one of the collection implementations
will satisfy your needs. If not, you can implement relevant collection in-
terfaces yourself to provide collections that satisfy your particular needs.
You will find skeletal implementations in the abstract classes AbstractCol-
lection , AbstractSet , AbstractList , AbstractSequentialList , AbstractQueue ,
and AbstractMap . You can extend these classes to create your own collec-
tions, often with less work than starting from the interfaces directly. The
concrete collections shown in Figure 21-1 on page 568 each extend the
appropriate abstract collection type, as shown in the concrete type tree
in Figure 21-1 .
These abstract collection classes are designed to be helpful superclasses
for your own collection implementations. They are not requiredin some
cases you will find it easier or more efficient to directly implement the
interfaces.
Each abstract collection class declares a few abstract methods and uses
them in default implementations of other methods of the collection type.
For example, AbstractList has two abstract methods: size and get . All
other querying methods of AbstractList , including its iterators, are imple-
mented by using these methods. You need only write your own imple-
mentation of the other methods if you want to, typically to either increase
efficiency or to allow something disallowed by default. For example, if
your list is modifiable, your subclass of AbstractList will have to provide
an overriding implementation of the set method, which by default throws
UnsupportedOperationException .
The root of the abstract collection types is AbstractCollection . If you need
a collection that isn't a set, list, or map you can subclass this directly.
Otherwise, you will probably find it more useful to subclass one of the
more specific abstract collections classes.
If the Collection class you are creating is unmodifiable (if the modification
methods of your collection should throw UnsupportedOperationException ),
your subclass of AbstractCollection need only provide an implementation
 
Search WWH ::




Custom Search