Wednesday 22 March 2017

The Legacy Classes and Interfaces - Java Tutorials ( Page - 1 of 3 )

As explained at the start of this chapter, the original version of java.util did not include the collections framework. Instead, it defined several classes and an interface that provided an ad hoc method of storing objects. With the addition of collections by Java 2, several of the original classes were reengineered to support the collection interfaces. Thus, they are fully compatible with the framework. While no classes have actually been deprecated, one has been rendered obsolete. Of course, where a collection duplicates the functionality of a legacy class, you will usually want to use the collection for new code. In general, the legacy classes are supported because there is still code that uses them.

One other point: None of the collection classes are synchronized, but all the legacy classes are synchronized. This distinction may be important in some situations. Of course, you can easily synchronize collections, too, by using one of the algorithms provided by Collections.

The legacy classes defined by java.util are shown here:
  1. Dictionary 
  2. Hashtable 
  3. Properties 
  4. Stack 
  5. Vector

There is one legacy interface called Enumeration. The following sections examine Enumeration and each of the legacy classes, in turn.


The Enumeration Interface

The Enumeration interface defines the methods by which you can enumerate (obtai one at a time) the elements in a collection of objects. This legacy interface has been superceded by Iterator. Although not deprecated, Enumeration is considered obsolete for new code. However, it is used by several methods defined by the legacy classes (such as Vector and Properties), is used by several other API classes, and is currently in widespread use in application code.

Enumeration specifies the following two methods:

      boolean hasMoreElements( )
      Object nextElement( )

When implemented, hasMoreElements( ) must return true while there are still more elements to extract, and false when all the elements have been enumerated. nextElement( ) returns the next object in the enumeration as a generic Object reference. That is, each call to nextElement( ) obtains the next object in the enumeration. The calling routine must cast that object into the object type held in the enumeration.


Vector

Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections framework. With the release of Java 2, Vector was reengineered to extend AbstractList and implement the List interface, so it now is fully compatible with collections.

Here are the Vector constructors:

      Vector( )
      Vector(int size)
      Vector(int size, int incr)
      Vector(Collection c)

The first form creates a default vector, which has an initial size of 10. The second form creates a vector whose initial capacity is specified by size. The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward. The fourth form creates a vector that contains the elements of collection c. This constructor was added by Java 2.

All vectors start with an initial capacity. After this initial capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the increment that you specify when you create the vector. If you don’t specify an increment, the vector’s size is doubled by each allocation cycle.

Vector defines these protected data members:

      int capacityIncrement;
      int elementCount;
      Object elementData[ ];

The increment value is stored in capacityIncrement. The number of elements currently in the vector is stored in elementCount. The array that holds the vector is stored in elementData.


The Methods Defined by Vector

void addElement(Object element):  The object specified by element is added to the vector.

int capacity( ):  Returns the capacity of the vector.

Object clone( ):  Returns a duplicate of the invoking vector.

boolean contains(Object element):  Returns true if element is contained by the vector, and returns false if it is not.

void copyInto(Object array[ ]):  The elements contained in the invoking vector are copied into the array specified by array.

Object elementAt(int index):  Returns the element at the location specified by index.

Enumeration elements( ):  Returns an enumeration of the elements in the vector.

void ensureCapacity(int size):  Sets the minimum capacity of the vector to size.

Object firstElement( ):  Returns the first element in the vector.

int indexOf(Object element):  Returns the index of the first occurrence of element. If the object is not in the vector, –1 is returned.

int indexOf(Object element, int start):  Returns the index of the first occurrence of element at or after start. If the object is not in that portion of the vector, –1 is returned.

void insertElementAt(Object element, int index):  Adds element to the vector at the location specified by index.

boolean isEmpty( ):  Returns true if the vector is empty and returns false if it contains one or more elements.

Object lastElement( ):  Returns the last element in the vector.

int lastIndexOf(Object element):  Returns the index of the last occurrence of element. If the object is not in the vector, –1 is returned.

int lastIndexOf(Object element, int start):  Returns the index of the last occurrence of element before start. If the object is not in that portion f the vector, –1 is returned.

void removeAllElements( ):  Empties the vector. After this method executes, the size of the vector is zero.

boolean removeElement(Object element):  Removes element from the vector. If more than one instance of the specified object exists in the vector, then it is the first one that is removed. Returns true if successful and false if the object is not found.

void removeElementAt(int index):  Removes the element at the location specified by index.

void setElementAt(Object element, int index):  The location specified by index is assigned element.

void setSize(int size):  Sets the number of elements in the vector to size. If the new size is less than the old size, elements are lost. If the new size is larger than the old size, null elements are added.

int size( ):  Returns the number of elements currently in the vector.

String toString( ):  Returns the string equivalent of the vector.

void trimToSize( ):  Sets the vector’s capacity equal to the number of elements that it currently holds.


Because Vector implements List, you can use a vector just like you use an ArrayList instance. You can also manipulate one using its legacy methods. For example, after you instantiate a Vector, you can add an element to it by calling addElement( ). To obtain the element at a specific location, call elementAt( ). To obtain the first element in the vector, call firstElement( ). To retrieve the last element, call lastElement( ). You can obtain the index of an element by using indexOf( ) and lastIndexOf( ). To remove an element, call removeElement( ) or removeElementAt( ).

The following program uses a vector to store various types of numeric objects. It demonstrates several of the legacy methods defined by Vector. It also demonstrates the Enumeration interface.

  // Demonstrate various Vector operations.
  import java.util.*;

  class VectorDemo {
    public static void main(String args[]) {

      // initial size is 3, increment is 2
      Vector v = new Vector(3, 2);

      System.out.println("Initial size: " + v.size());
      System.out.println("Initial capacity: " +
                         v.capacity());

      v.addElement(new Integer(1));
      v.addElement(new Integer(2));
      v.addElement(new Integer(3));
      v.addElement(new Integer(4));

      System.out.println("Capacity after four additions: " +
                         v.capacity());
      v.addElement(new Double(5.45));
      System.out.println("Current capacity: " +
                         v.capacity());
      v.addElement(new Double(6.08));
      v.addElement(new Integer(7));

      System.out.println("Current capacity: " +
                         v.capacity());
      v.addElement(new Float(9.4));
      v.addElement(new Integer(10));

      System.out.println("Current capacity: " +
                         v.capacity());
      v.addElement(new Integer(11));
      v.addElement(new Integer(12));

      System.out.println("First element: " +
                        (Integer)v.firstElement());

      System.out.println("Last element: " +
                        (Integer)v.lastElement());

      if(v.contains(new Integer(3)))
        System.out.println("Vector contains 3.");
  
      // enumerate the elements in the vector.
      Enumeration vEnum = v.elements();

      System.out.println("\nElements in vector:");
      while(vEnum.hasMoreElements())
        System.out.print(vEnum.nextElement() + " ");
      System.out.println();
    }
  }

The output from this program is shown here:

  Initial size: 0
  Initial capacity: 3
  Capacity after four additions: 5
  Current capacity: 5
  Current capacity: 7
  Current capacity: 9
  First element: 1
  Last element: 12
  Vector contains 3.

  Elements in vector:
  1 2 3 4 5.45 6.08 7 9.4 10 11 12

With the release of Java 2, Vector added support for iterators. Instead of relying on an enumeration to cycle through the objects (as the preceding program does), you now can use an iterator. For example, the following iterator-based code can be substituted into the program:

  // use an iterator to display contents
  Iterator vItr = v.iterator();

  System.out.println("\nElements in vector:");
  while(vItr.hasNext())
    System.out.print(vItr.next() + " ");
  System.out.println();

Because enumerations are not recommended for new code, you will usually use an iterator to enumerate the contents of a vector. Of course, much legacy code exists that employs enumerations. Fortunately, enumerations and iterators work in nearly the same manner.

No comments:

Post a Comment