Skip to content

Commit 0054ea9

Browse files
authored
Merge pull request #190 from javaevolved/brunoborges-modern-synchronized-collections
Add modern synchronized collections pattern
2 parents 64db8d6 + 74aea9c commit 0054ea9

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
id: d54a4e9f-74b9-4579-84a3-2541fe7b4272
3+
slug: "legacy-synchronized-collections"
4+
title: "Legacy synchronized collections to modern alternatives"
5+
category: "collections"
6+
difficulty: "intermediate"
7+
jdkVersion: "5"
8+
oldLabel: "Legacy synchronized collection"
9+
modernLabel: "Concurrent Collections API"
10+
oldApproach: "Hashtable"
11+
modernApproach: "ConcurrentHashMap"
12+
oldCode: |-
13+
Hashtable<String, Session> sessions = new Hashtable<>();
14+
sessions.put(id, session);
15+
modernCode: |-
16+
ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>();
17+
sessions.put(id, session);
18+
summary: "Replace Vector and Hashtable with collections selected for actual mutability and concurrency needs."
19+
explanation: "Vector and Hashtable synchronize every operation through legacy APIs. Modern\
20+
\ code should normally use ArrayList or HashMap for thread-confined data, and purpose-built\
21+
\ concurrent collections when shared mutation is required."
22+
whyModernWins:
23+
- icon: "🎯"
24+
title: "Intentional concurrency"
25+
desc: "The chosen type documents whether sharing is expected."
26+
- icon: ""
27+
title: "Better scalability"
28+
desc: "ConcurrentHashMap avoids a single table-wide lock for normal access."
29+
- icon: "🧩"
30+
title: "Modern APIs"
31+
desc: "Works naturally with current collection and concurrent-map operations."
32+
support:
33+
state: "available"
34+
description: "Widely available since JDK 5 (September 2004)"
35+
prev: "collections/unmodifiable-collectors"
36+
next: "strings/string-isblank"
37+
related:
38+
- "collections/immutable-map-creation"
39+
- "collections/copying-collections-immutably"
40+
- "concurrency/lock-free-lazy-init"
41+
tags:
42+
- collections
43+
- concurrency
44+
docs:
45+
- title: "ConcurrentHashMap"
46+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/ConcurrentHashMap.html"
47+
- title: "ConcurrentMap"
48+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/concurrent/ConcurrentMap.html"

content/collections/unmodifiable-collectors.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ support:
3636
state: "available"
3737
description: "Widely available since JDK 16 (March 2021)"
3838
prev: "collections/reverse-list-iteration"
39-
next: "strings/string-isblank"
39+
next: "collections/legacy-synchronized-collections"
4040
related:
4141
- "collections/immutable-map-creation"
4242
- "collections/immutable-set-creation"

content/strings/string-isblank.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ whyModernWins:
3232
support:
3333
state: "available"
3434
description: "Widely available since JDK 11 (Sept 2018)"
35-
prev: "collections/unmodifiable-collectors"
35+
prev: "collections/legacy-synchronized-collections"
3636
next: "strings/string-strip"
3737
related:
3838
- "strings/string-formatted"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
import java.util.concurrent.ConcurrentHashMap;
4+
import java.util.concurrent.ConcurrentMap;
5+
6+
/// Proof: legacy-synchronized-collections
7+
/// Source: content/collections/legacy-synchronized-collections.yaml
8+
void main() {
9+
String id = "session-1";
10+
Session session = new Session();
11+
12+
ConcurrentMap<String, Session> sessions = new ConcurrentHashMap<>();
13+
sessions.put(id, session);
14+
}
15+
16+
record Session() {}

0 commit comments

Comments
 (0)