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 aListdeclared against the interface) instead ofVector; -
ArrayDequeused as a stack (push/pop/peek) instead ofStack.
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.