Commit 413b14d
committed
feat(ConcurrentHashMapDemo): add minimal example with detailed internal notes
What
- Added ConcurrentHashMapDemo class.
- Created a ConcurrentHashMap<String, Integer>.
- Inserted two key-value pairs ("World" → 2, "Hello" → 1).
- Printed map contents.
Why
- Demonstrates how to use ConcurrentHashMap with a minimal example.
- Preserves original simple logic while adding educational documentation.
- Explains internal implementation differences between Java 7 and Java 8+.
How
- map.put() safely adds entries, supporting concurrency without explicit synchronization.
- System.out.println(map) prints current contents (unordered).
- Documentation added to describe segment-based locking (Java 7) vs CAS-based design (Java 8).
Logic
- Input: none (map entries hardcoded).
- Processing: thread-safe puts into the ConcurrentHashMap.
- Output: printed contents of the map.
- Order of keys is non-deterministic because ConcurrentHashMap does not guarantee ordering.
Real-life applications
- Thread-safe caches.
- Shared counters or statistics maps.
- Concurrent access to maps without needing synchronized blocks.
Notes
- Use ConcurrentHashMap when multiple threads read/write frequently.
- Not ordered: if you need order, use ConcurrentSkipListMap instead.
- Avoid storing null keys or values (not supported in ConcurrentHashMap).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 75ed329 commit 413b14d
File tree
1 file changed
+40
-0
lines changed- Section 25 Collections Frameworks/Map Interface/Concurrent HashMap/src
1 file changed
+40
-0
lines changedLines changed: 40 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
0 commit comments