CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Collections

Collections and Generics
Legacy collections
Introduction

An introduction to legacy collection classes

  • The collection hierarchy was introduced with Java 1.2.
  • For compatibility reasons, Java still supports older collection classes such as Vector, HashTable, and Stack. However, you should avoid using these classes for new software development.
  • The Vector, HashTable, and Stack classes aren't deprecated. They are still fully supported as part of the Java API.
  • The Vector class was the most commonly used legacy collection class. The newer ArrayList class is an improved version of the Vector class, so the code used to work with a vector is similar to the code used to work with an array list.

Legacy collection classes

Class Description
Vector Provides features similar to the more powerful ArrayList class.
HashTable Provides features similar to the more powerful HashMap class.
Stack A type of Vector that implements a stack, which is a last-in, first-out list. The LinkedList class is now the preferred class for implementing a stack.

The Vector Class

In Java 2, Vector is the same as ArrayList, except that Vector contains the synchronized methods for accessing and modifying the vector. None of the new collection data structures introduced so far are synchronized. If synchronization is required, you can use the synchronized versions of the collection classes.

Code that uses a vector

// create a vector
Vector codes = new Vector();

// add three strings
codes.add("mbdk");
codes.add("citr");
codes.add(0, "warp");

// print the vector
for (int i =0; i < codes.size(); i++)
{
    String code = (String)codes.get(i);
    System.out.println(code);
}

Resulting output

warp
mbdk
citr
Previous | Introduction | Untype collection | Wrapper classes