THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Of these types, only Properties is in active useit is used to contain system properties, as described in
"System Properties" on page 663 and by some applications to store configuration data. We describe the other
legacy collections by contrasting them with their analogous types. We then describe Properties in more detail
since you are more likely to actually need to write code that uses it.


21.15.1. Enumeration


Enumeration is analogous to Iterator, but has just two methods: hasMoreElements which
behaves like hasNext, and nextElement which behaves like next. You can get an Enumeration that
iterates through a non-legacy collection from the static method Collections.enumeration.


You can convert an Enumeration to a List via the static Collections.list method, which returns
an ArrayList containing all the elements accessible by the enumeration, in the order the enumeration
returned them.


Exercise 21.6: Rewrite the example program Concat on page 528 so that it uses an implementation of
Enumeration that has only one FileInputStream object open at a time.


21.15.2. Vector


The Vector class is analogous to ArrayList. Although Vector is a legacy class, it has been
made to implement List, so it works as a Collection. All methods that access the contents of a Vector
are synchronized. As a legacy collection, Vector contains many methods and constructors that are
analogous to those of ArrayList, in addition to those it inherits from List. The legacy constructors and
methods of Vector and their analogues in ArrayList are


publicVector(int initialCapacity, int capacityIncrement)

No analogue. Creates a new Vector with the given initialCapacity
that will grow by capacityIncrement elements at a time.

publicVector(int initialCapacity)

Analogous to ArrayList(initialCapacity).

publicVector()

Analogous to ArrayList().

publicVector(Collection<? extends E> coll)

Analogous to ArrayList(coll).

public final voidaddElement(E elem)

Analogous to add(elem).

public final voidinsertElementAt(E elem, int index)

Analogous to add(index,elem).

public final voidsetElementAt(E elem, int index)
Free download pdf