Combine private staging with replay-aware insertion. A batch may contain new entries, exact repeats and one conflicting ID. The required rule is all-or-nothing: a conflict anywhere rejects the entire batch, including new entries encountered earlier in that batch.
Understand the contract
Write a transition table before coding: absent key means stage an insertion; equal stored value means do nothing; different stored value under the same key means reject. Use the candidate map for every lookup. This naturally handles repeats within a batch as well as repeats against older state.
Do not update a separate total incrementally while staging. Derive reports from the committed map after success so a rejected candidate cannot leave a stale aggregate. A count of inserted entries, if needed, should only be published with an accepted batch. The result type can make that distinction explicit later in the ledger.
The worked examples below inspect whole maps so you can see both keys and values. LinkedHashMap gives deterministic insertion order for this demonstration. The original and candidate are independent containers, and the immutable Integer values are safe to share.
Before running, write the output you expect and explain which rule produces each line. Each editor is an independent program.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> current = new LinkedHashMap<>();
current.put("A", 10);
Map<String, Integer> candidate = new LinkedHashMap<>(current);
candidate.put("B", 20);
System.out.println(current);
System.out.println(candidate);
}
}Check your prediction
{A=10}
{A=10, B=20}Trace a different case
Map.equals compares mappings, not insertion order or object identity. That makes it useful for checking state preservation after rejection, but it is not a complete test of a promised iteration order. Use an ordered key list as an additional expectation when order itself is part of the contract. Here we compare independent maps with the same contents.
import java.util.Map;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
Map<String, Integer> first = new LinkedHashMap<>();
first.put("A", 10);
first.put("B", 20);
Map<String, Integer> second = new LinkedHashMap<>();
second.put("B", 20);
second.put("A", 10);
System.out.println(first.equals(second));
System.out.println(first == second);
}
}Check your prediction
true
falsePlan your acceptance cases
Before writing the complete implementation, predict these cases without running the solution. Keep the requirements above as the authority; this table highlights distinct promises rather than prescribing a particular class layout.
| Case | Expected behavior | Skill it checks |
|---|---|---|
| New B followed by conflicting A | Original map unchanged | Late rollback |
| New B twice with equal content | One mapping | Replay within the candidate |
| New B twice with different content | Reject complete candidate | Within-batch conflict |
After your first version works, choose one row and introduce a plausible defect that it should expose. Restore the correct version, then recreate the central operation without the worked example. If one skill is missing, revisit that lesson before combining more features.
Build it yourself
Checkpoint: Safe Batch: practice
Implement stage(Map<String,Integer>, List<Entry>) returning a new LinkedHashMap on success. Entry is a record of nonnull id and int value. Identical replays do nothing; conflicts throw before the original changes. From {A=10}, stage B/20 and A/10 to obtain {A=10,B=20}. Then stage C/30 and A/99 and prove the original still equals {A=10}.
public class Main {
public static void main(String[] args) {
// Add your implementation and checks below.
System.out.println("Your workspace");
}
}Hint 1: the idea
The complete batch must succeed before its candidate can become the current state.
Hint 2: a first step
Look up each ID in candidate, compare its value if present, and put only absent IDs.
Show one solution
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
Map<String, Integer> current = new LinkedHashMap<>();
current.put("A", 10);
System.out.println(stage(current, List.of(new Entry("B", 20), new Entry("A", 10))));
try {
stage(current, List.of(new Entry("C", 30), new Entry("A", 99)));
throw new AssertionError("conflict accepted");
} catch (IllegalArgumentException expected) {
if (!current.equals(Map.of("A", 10))) {
throw new AssertionError("partial change escaped");
}
}
System.out.println(current);
}
static Map<String, Integer> stage(Map<String, Integer> current, List<Entry> batch) {
Map<String, Integer> candidate = new LinkedHashMap<>(current);
for (Entry entry : batch) {
Integer old = candidate.get(entry.id());
if (old == null) {
candidate.put(entry.id(), entry.value());
} else if (old.intValue() != entry.value()) {
throw new IllegalArgumentException("id conflict");
}
}
return candidate;
}
}
record Entry(String id, int value) {}Expected output:
{A=10, B=20}
{A=10}Each entry falls into one of three cases: absent, so put it; present with the
same value, so it is a replay and nothing happens; present with a different
value, so throw. The null test must come first: get returns null for an
absent ID, and unboxing it with intValue() would throw NullPointerException.
All work happens on the candidate, so
the late A/99 conflict leaves current at {A=10} even though C was staged.
Change it and test again
Add a within-batch replay B/20,B/20 and a within-batch conflict B/20,B/21. An empty batch should return an equal independent map. Mutate the candidate initialization to alias current: the late-conflict test must expose a leaked C. Restore copying before moving on.
Explain before moving on
Where is the commit point in a caller that assigns the returned candidate? Which invariant would be broken by a caller that catches the conflict and uses a partially built map anyway? Explain why this policy differs deliberately from intermediate's recover-per-line importer.
Completion is saved in this browser only. It does not measure mastery.