Replace Vector With List

ID

java.replace_vector_list

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Java

Tags

efficiency

Description

Reports fields, local variables and parameters declared as java.util.Vector or java.util.Stack.

Rationale

Vector and Stack are legacy JDK 1.0 collections: every method is synchronized, so callers pay the monitor cost even in strictly single-threaded code, and both expose awkward enumeration-era APIs (elements(), addElement, …).

Since JDK 1.2 the preferred replacements are:

  • ArrayList (or a List declared against the interface) instead of Vector;

  • ArrayDeque used as a stack (push/pop/peek) instead of Stack.

If genuine thread-safety is needed, use Collections.synchronizedList(new ArrayList<>()) or, better, a java.util.concurrent collection — both keep the synchronization intentional and visible.

Remediation

// Instead of
private Vector<String> items = new Vector<>();

// Use
private List<String> items = new ArrayList<>();