Java Reference
In-Depth Information
Display 16.6
HashSet<T>
Class Demo
(part 3 of 3)
ball in set round? true
ball in set green? false
ball and peas in same set? true
pie and grass in same set? false
Union of green and round:
garden hose grass peas ball pie grapes
Intersection of green and round:
peas grapes
If you want a class that implements the
List<T>
interface and do not need any methods
beyond those in the
List<T>
interface, you can use the
ArrayList<T>
or
Vector<T>
class.
So, after all is said and done, if all you need is a collection class that allows elements to
occur more than once, or you need a collection that orders its elements as on a list (that is,
as in an array), or you need a class that has both of these properties, then you can use the
ArrayList<T>
or
Vector<T>
class and need not worry about all the other classes and inter-
faces in Display 16.1. The
ArrayList<T>
and
Vector<T>
classes implement all the meth-
ods in the
List<T>
interface. A table of methods for the
ArrayList<T>
class was given in
Chapter 14 and a more complete table is given in Display 16.7. A table of methods for the
Vector<T>
class is also given in Display 16.7. A more complete list of the methods in
these classes is given in Appendix 5. If you want to define your own class that implements
the
List<T>
interface, you would probably be better off using either the
ArrayList<T>
or the
Vector<T>
class rather than the
AbstractList<T>
class as a base class.
The abstract class
AbstractSequentialList<T>
is derived from the
AbstractList<T>
class. Although it does override some methods inherited from the class
AbstractList<T>
,
it adds no completely new methods. The point of the
AbstractSequentialList<T>
class
is to provide for efficient implementation of sequentially moving through the list at the
expense of having inefficient implementation of random access to elements (that is, ineffi-
cient implementation of the
get
method). The
LinkedList<T>
class is a concrete derived
class of the abstract class
AbstractSequentialList<T>
. (The implementation of the
LinkedList<T>
class is similar to that of the linked list classes we discussed in Chapter 15.)
If you need a
List<T>
with efficient random access to elements (that is, efficient imple-
mentation of the
get
method), then use the
ArrayList<T>
or
Vector<T>
class or a class
derived from one of these two classes. If you do not need efficient random access but need
to efficiently move sequentially through the list, then use the
LinkedList<T>
class or a
class derived from the
LinkedList<T>
class.
The interface
SortedSet<T>
and the concrete class
TreeSet<T>
are designed for imple-
mentations of the
Set<T>
interface that provide for rapid retrieval of elements (efficient
implementation of the
contains
and similar methods). The implementation of the class is
similar to the binary tree class discussed in Chapter 15 but with more sophisticated ways to
do inserting that keep the tree balanced. We will not discuss the
SortedSet<T>
interface or
Vector<T>
Abstract-
Sequential-
List<T>
Linked-
List<T>
SortedSet<T>
TreeSet<T>