Skip to content

Commit 741f33e

Browse files
committed
feat(HashtableComputeDemo): add demo of compute() and computeIfAbsent() in Hashtable
What - Added `HashtableComputeDemo` to showcase the `compute` and `computeIfAbsent` methods. - Inserted initial key–value pairs: (1 → "A"), (2 → "B"), (3 → "C"). - Recomputed value at key `2` with `compute()`, appending `"Z"` to `"B"`. - Added new entry (7 → "Z7") with `computeIfAbsent()`. Why - To demonstrate how modern Map API methods work with legacy synchronized `Hashtable`. - `compute` allows atomic recomputation of a mapping based on its current value. - `computeIfAbsent` is useful for lazy initialization of missing keys. How - Step 1: Populate hashtable with three entries. - Step 2: Call `ht.compute(2, (k, v) -> v + "Z")`: - Lambda receives key=2 and value="B". - Returns "BZ", which replaces old value. - Step 3: Call `ht.computeIfAbsent(7, k -> "Z"+k)`: - Since key 7 was absent, computes "Z7" and inserts (7 → "Z7"). - Step 4: Print table before and after recomputations. Logic - Inputs: integer keys, string values. - Outputs: - Before: `{3=C, 2=B, 1=A}` - After: `{3=C, 2=BZ, 1=A, 7=Z7}` - Flow: 1. Initial table with 3 entries. 2. `compute()` modifies existing entry at key 2. 3. `computeIfAbsent()` inserts a new entry for missing key 7. Real-life applications - Caching: lazily populate values only when needed. - Counters or accumulators: recompute value based on existing. - Thread-safe updates in legacy systems using `Hashtable`. Notes - `Hashtable` is synchronized, so these methods are thread-safe but slower than modern alternatives. - `compute` can overwrite existing mappings. - `computeIfAbsent` only runs the lambda when key is missing. - Modern recommendation: prefer `ConcurrentHashMap` or `HashMap` unless legacy compatibility required. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 9bcf1f1 commit 741f33e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Hashtable;
2+
3+
public class HashtableComputeDemo {
4+
public static void main(String[] args) {
5+
Hashtable<Integer, String> ht = new Hashtable<>();
6+
7+
ht.put(1, "A");
8+
ht.put(2, "B");
9+
ht.put(3, "C");
10+
11+
System.out.println("Before compute: " + ht);
12+
13+
// compute: recompute value for key 2 based on its current value
14+
// lambda (k, v) receives key and current value v (non-null for Hashtable)
15+
ht.compute(2, (k, v) -> v + "Z"); // "B" -> "BZ"
16+
17+
// computeIfAbsent: if key 7 absent, compute and put "Z7"
18+
ht.computeIfAbsent(7, (k) -> "Z" + k);
19+
20+
System.out.println("After compute & computeIfAbsent: " + ht);
21+
22+
/*
23+
- compute(2, (k,v) -> v + "Z") : agar key 2 pe value thi (v="B"),
24+
to lambda return karega "BZ" aur woh new value ban jayegi.
25+
- computeIfAbsent(7, k -> "Z"+k) : agar key 7 nahi hai tab hi value "Z7" insert hogi.
26+
*/
27+
}
28+
}

0 commit comments

Comments
 (0)