Java Reference
In-Depth Information
You can use
search
to find an object's distance from the top of the stack,
with 1 being the top of the stack. If the object isn't found, 1 is returned.
The
search
method uses
Object.equals
to test whether an object in the
stack is the same as the one it is searching for.
These methods are trivial uses of the
Vector
methods, so using an
Ar-
rayList
to implement a
Stack
would be simple: using
add
to implement
push
, and so on. There is no analogue to
Stack
in the collections.
Exercise 21.7
: Implement a stack using
ArrayList
. Should your stack
class be a subclass of
ArrayList
or use an
ArrayList
internally, providing
different stack-specific methods?
21.15.4.
Dictionary
The
Dictionary<K,V>
abstract class is essentially an interface. It is ana-
logous to the
Map
interface but uses the terms
key
and
element
instead
of
key
and
value.
With two exceptions, each method in
Dictionary
has
the same name as its analogous method in
Map
:
get
,
put
,
remove
,
size
,
and
isEmpty
. The two exceptions are
keys
and
elements
. In
Map
, you get a
Set
of keys or values that you can iterate over or otherwise manipulate.
In
Dictionary
, you can only get an
Enumeration
(iterator) for the keys and
elements, using the
keys
and
elements
methods, respectively. The legacy
collections did not contain a
Set
type, and so
Dictionary
could not be ex-
pressed in those terms.
21.15.5.
Hashtable
The
Hashtable<K,V>
class is similar to the
HashMap
class, but implements
the methods of
Dictionary
as well as (more recently) implementing the
Map
interface. All methods of
Hashtable
are synchronized, unlike
HashMap
.
Beyond the methods inherited from
Dictionary
and
Map
,
Hashtable
adds
the following methods and constructors:
public
Hashtable()