Replace Hashtable With Map

ID

java.replace_hashtable_map

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

Java

Tags

efficiency

Description

Reports declarations typed as java.util.Hashtable.

Rationale

Hashtable is a legacy JDK 1.0 type. Every method is synchronized, which means callers pay a monitor cost on every access even in strictly single-threaded code. Hashtable also rejects null keys and values, while HashMap accepts them.

When genuine thread-safety is required, java.util.concurrent.ConcurrentHashMap gives finer-grained locking and better scalability than Hashtable.

Remediation

// Instead of
private Hashtable<String, Integer> counts = new Hashtable<>();

// Use
private Map<String, Integer> counts = new HashMap<>();

// Or, when shared across threads
private Map<String, Integer> counts = new ConcurrentHashMap<>();