Skip to content

[Pattern] Legacy synchronized collections to modern alternatives #180

Description

@brunoborges

Category

collections

Slug

legacy-synchronized-collections

Title

Legacy synchronized collections to modern alternatives

Difficulty

intermediate

Since JDK

5

Summary

Replace Vector and Hashtable with collections selected for actual mutability and concurrency needs.

Old code label

Legacy synchronized collection

Old code

Hashtable<String, Session> sessions = new Hashtable<>();
sessions.put(id, session);

Modern code label

Concurrent Collections API

Modern code

ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>();
sessions.put(id, session);

Explanation

Vector and Hashtable synchronize every operation through legacy APIs. Modern code should normally use ArrayList or HashMap for thread-confined data, and purpose-built concurrent collections when shared mutation is required.

Why the modern way wins

🎯 Intentional concurrency — The chosen type documents whether sharing is expected.
⚡ Better scalability — ConcurrentHashMap avoids a single table-wide lock for normal access.
🧩 Modern APIs — Works naturally with current collection and concurrent-map operations.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    slugNew or updated pattern snippet (category/slug.json)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions