Learn from Mistakes: A Quick Guide from 100 Java Mistakes and How to Avoid Them
Learn a little today, avoid a lot of bugs tomorrow
The book 100 Java Mistakes and How to Avoid Them is a great book for developers who want to grow faster by learning from real-world problems. Juniors and mid-level engineers will benefit the most, but even experienced developers will find surprising pitfalls they may have never seen before.
In this short guide, we look at one example from the book, the unexpected behavior of the peek method, and how a small detail can change the output of your code.
A Peek Problem You Might Not Expect
public static void main(String[] args) {
AtomicLong counter = new AtomicLong();
long count = Stream.of(1,2,3,4,5,6,7,8,9)
.peek(p -> counter.incrementAndGet())
.count();
System.out.println(count);
System.out.println(counter.get());
}What is the output?
Answer: it depends.
Java 8 output
9
9Java 9 or higher
9
0From Java 9 onward, count() can detect that the stream source has a known size.
Because of this optimization, map() and peek() may not run at all, and the runtime simply returns the list’s size.
Why This Happens
The documentation clearly states:
peek() exists mainly to support debugging
It is not guaranteed to run. It was never designed for side effects.
Why This Book Helps You Level Up 📘
The book 100 Java Mistakes and How to Avoid Them contains a collection of mistakes from real scenarios.
Many are common, but some are deep and subtle, the kind you only learn after years of debugging or reading the right material.
Tagir Valeev explains each mistake clearly, shows why it happens, and teaches you how to avoid it.
Even if you fix only one bad habit, the book pays for itself.
Conclusion
Learning from other developers’ mistakes is one of the fastest ways to grow.
The peek() example is a reminder that even familiar features can behave differently than we expect.
If you enjoyed this breakdown, subscribe or share the newsletter, it helps me bring more practical Java content every week. 🚀


