The nightly import
read every supplier's catalogue
into one lookup.
Code to description.
One tidy line:
Map bySku = products.stream()
.collect(Collectors.toMap(Product::sku, Product::description));
It ran every night
for a year.
Then one morning
the website showed
yesterday's prices.
And the day before's.
The import had died
on its first catalogue
and taken all the others with it.
The log had one line
worth reading:
IllegalStateException:
Duplicate key BR-1040.
A new supplier
used the same code
as an old one.
Here is the mechanism.
toMap without a third argument
has no opinion
about duplicates.
So it refuses.
Not on the duplicate row.
On the whole stream.
Nothing is collected.
Nothing is saved.
A week later
it failed again,
differently.
NullPointerException.
One product
had no description.
That one surprises people,
because a HashMap
is perfectly happy
to hold a null value.
toMap is not.
It checks every value
and throws on the first null,
whatever map
it is filling.
Neither failure
showed up in testing.
The test catalogue
was small,
clean,
and written by us.
Real catalogues
are written by suppliers.
Suppliers do not know
your map exists.
They only know
their own codes.
The fix is not
to make the errors go away.
It is to decide
what a duplicate means,
out loud,
in the code:
.collect(Collectors.toMap(
Product::sku,
Product::description,
(kept, dropped) -> kept));
Keep the first.
Or keep the newest.
Or collect them all
with groupingBy
and let a person choose.
Each is a business rule.
The original line
had a rule too.
It was:
stop everything.
Nobody chose it.
And for nulls,
decide before the collector,
not inside it.
Filter them out
and count them.
Or replace them
with something
a person will notice.
Then put one rotten row
in your test data.
A duplicate.
A blank.
A code with a space at the end.
A collector
is a decision
written very small.
Read it
as if it could say no.
– Serguey Asael Shinder