C

Core Java tutorial for beginners

Clean • Professional

Legacy Collections in Java – Vector, Stack, Hashtable, Properties & More

3 minute

Legacy Collections in Java

Legacy Collections are the oldest data structure classes in Java, created before Java 1.2—before the modern Collection Framework was introduced.

Even though Java later added the Collection Framework, these classes were kept for backward compatibility, and most of them were redesigned internally to work with the new framework.

All legacy classes are available in the java.util package.


What Are Legacy Collections?

Legacy Collections refer to these old classes and interfaces:

learn code with durgesh images

  • Vector
  • Stack
  • Hashtable
  • Properties
  • Enumeration (legacy iterator)

These classes:

  • Are synchronized (thread-safe by default)
  • Belong to JDK 1.0
  • Have older method names like addElement()
  • Use Enumeration instead of Iterator
  • Are slower than modern alternatives
  • Still supported—but not recommended for new projects

1. Vector (Legacy List Implementation)

What is Vector?

Vector is a dynamic array similar to ArrayList, but it is synchronized, making it thread-safe but slower.

Iterable
   └── Collection
          └── List
                └── Vector

Key Points

  • Maintains insertion order
  • Allows duplicates
  • Thread-safe → slower than ArrayList
  • Part of List hierarchy

Example

import java.util.*;

Vector<String> vec = new Vector<>();
vec.add("Java");
vec.add("Python");
vec.add("C++");

System.out.println(vec);

2. Stack (Legacy LIFO Implementation)

What is Stack?

Stack is a legacy LIFO (Last-In-First-Out) data structure that extends Vector.

Key Points

  • Built on top of Vector
  • Fully synchronized
  • Works on push/pop operations

Example

import java.util.*;

Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);

System.out.println(stack.pop()); // 30

Modern Replacement:

Use ArrayDeque instead (much faster and recommended).


3. Hashtable (Legacy Map Implementation)

What is Hashtable?

Hashtable is a synchronized Map implementation that stores data in key-value pairs.

Map
   └── Hashtable

Key Points

  • Thread-safe
  • Does not allow null key or null values
  • Slower than HashMap
  • Legacy alternative to HashMap

Example

import java.util.*;

Hashtable<Integer, String> table = new Hashtable<>();
table.put(1, "Java");
table.put(2, "Python");

System.out.println(table.get(1));

Modern Replacement:

Use HashMap or ConcurrentHashMap (better performance).


4. Enumeration (Legacy Iterator)

What is Enumeration?

Enumeration is an old interface used to iterate over legacy classes like Vector and Hashtable.

Key Points

  • Forward-only traversal
  • Cannot remove elements
  • Used only in legacy classes
  • Replaced by Iterator & ListIterator

Example

import java.util.*;

Vector<String> vec = new Vector<>();
vec.add("Java");
vec.add("C++");

Enumeration<String> en = vec.elements();

while (en.hasMoreElements()) {
    System.out.println(en.nextElement());
}

5. Properties (Configuration Storage Class)

What is Properties?

Properties is a specialized class (extends Hashtable) used to store String–String key-value pairs, mostly for configuration files.

Key Points

  • Keys & values stored as Strings only
  • Used for .properties config files
  • Common in Spring, JDBC, and Java applications

Example

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Properties p = new Properties();

        p.setProperty("username", "admin");
        p.setProperty("password", "12345");

        p.store(new FileOutputStream("config.properties"), "Config File");
    }
}

Why Are They Called "Legacy"?

Because they:

  • Were created before the modern Collection Framework
  • Use old method names (addElement(), removeElement())
  • Use Enumeration instead of Iterator
  • Are synchronized by default → slower
  • Have outdated design
  • Replaced by newer classes like ArrayList, HashMap, ArrayDeque

When Should You Use Legacy Collections?

Use them only when:

  • Working on older (legacy) codebases
  • You specifically need built-in synchronized operations

Otherwise → avoid them.


Points to Remember

Legacy ClassDescriptionModern Replacement
VectorSynchronized dynamic arrayArrayList
StackLIFO (extends Vector)ArrayDeque
HashtableSynchronized Map, no nullsHashMap / ConcurrentHashMap
EnumerationLegacy iteratorIterator / ListIterator
PropertiesConfig storage (String–String)Still used (no replacement)


Article 0 of 0