Quick revision
The short answer
The common Optional anti-patterns are calling get() without a check, writing isPresent() followed by get() instead of using map, ifPresent or orElse, returning null from a method declared to return Optional, using Optional for fields, parameters or collection elements, returning Optional<List<T>> instead of an empty list, and wrapping a value in Optional only to replace a simple local null check. Each one either brings back the NullPointerException that Optional was meant to prevent or adds allocation and clutter without making the code safer.
What to remember
In this answer, we saw that Optional is meant as a return type that makes absence visible, and that the common anti-patterns all work against that purpose. Calling get() without a check, or pairing isPresent() with get(), brings back the crash or the old null check; returning null from an Optional method breaks the caller's trust; Optional fields, parameters and collection elements add allocation and clutter; Optional<List<T>> duplicates what an empty list already says; and wrapping a local value only to avoid an if statement adds cost without informing anyone.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The common Optional anti-patterns are calling get() without a check, writing isPresent() followed by get() instead of using map, ifPresent or orElse, returning null from a method declared to return Optional, using Optional for fields, parameters or collection elements, returning Optional<List<T>> instead of an empty list, and wrapping a value in Optional only to replace a simple local null check. Each one either brings back the NullPointerException that Optional was meant to prevent or adds allocation and clutter without making the code safer.
Why this question is asked
Optional arrived in Java 8 with a clear purpose: a return type that tells the caller "there may be no result here, handle it". Many codebases then used it in ways that defeat that purpose, and reviewers keep seeing the same mistakes. Interviewers ask this question to check whether we understand what Optional is for, not only which methods it has. In this answer, we will recap what Optional is meant to do and then walk through six anti-patterns one at a time, each with the broken code first, the reason it is a problem, and the better version.
1A Quick Recap of What Optional Is For
Optional<T> is a container that either holds one non-null value or is empty. We create it with Optional.of(value), Optional.ofNullable(maybeNull) or Optional.empty(). Its designers intended it mainly as a method return type for cases where "no result" is a normal outcome, such as looking up a user that may not exist.
The value of Optional is that it makes absence visible in the method signature and offers methods to deal with it: map transforms the value if present, orElse and orElseGet supply a fallback, orElseThrow fails with a clear exception, and ifPresent runs code only when there is a value. Every anti-pattern below either skips these methods or uses Optional where there is no caller to inform.
| Anti-pattern | Why it hurts | Better |
|---|---|---|
opt.get() with no check | Throws like a null dereference | orElseThrow(), orElse, map |
isPresent() then get() | A null check in disguise | map, ifPresent, orElse |
| return null from Optional method | Caller gets an NPE on the Optional | return Optional.empty() |
| Optional fields, parameters, elements | Allocation, clutter, not Serializable | Nullable field, overloads |
Optional<List<T>> | Two ways to say "nothing" | Return an empty list |
| Optional for a local null check | Allocation for no benefit | A plain if statement |
2Calling get() Without Checking
The most common mistake is to call get() on an Optional without knowing that it holds a value.
This is exactly the failure Optional was meant to prevent, only with a different exception name. The code reads as if absence were impossible, which hides the decision the author should have made. If absence really is a bug at this point, say so explicitly with orElseThrow(), which has the same behaviour but whose name states the intent (Java 10 added the no-argument form, and it is the recommended replacement for get()). If absence is a normal outcome, handle it with orElse, orElseGet or map, as the next section shows.
It also helps to pass a supplier to orElseThrow so the failure carries meaning, for example user.orElseThrow(() -> new UserNotFoundException("u-7")). The stack trace then names the business problem instead of a generic "No value present", which saves time for whoever reads the log later.
3Using isPresent() Followed by get()
The next pattern is safe but misses the point: it turns Optional back into an old-style null check.
The reason this matters is not only length. The isPresent and get pair keeps a get() call in the code, and during later edits someone moves or copies it without the check. The functional methods can never be called on an empty Optional by mistake, so the safety is built in rather than depending on the order of two lines.
4Returning null From a Method That Returns Optional
Sometimes a method declared to return Optional ends up returning null, often from an early exit or a cache miss.
Callers trust the signature: if a method returns Optional, they call methods on the result without checking it for null. Returning null breaks that contract in the worst possible way, because the caller who did everything right still crashes. The fix is always Optional.empty(), or an exception if a null argument is a programming error.
5Using Optional for Fields, Parameters and Collection Elements
Optional was not designed to be stored. It is not Serializable, so a class with an Optional field cannot be serialized with Java serialization, and JPA providers do not map Optional fields. Each Optional is also an extra object on the heap, so a field of type Optional<String> costs an additional allocation for every instance, and a List<Optional<Order>> doubles the number of objects compared with simply leaving the missing ones out.
For parameters, Optional pushes work onto every caller, who must write process(Optional.of(x)) or process(Optional.empty()), and it does not even guarantee anything, because a caller can still pass null for the Optional itself. Overloads or a @Nullable annotation express an optional argument more clearly. For fields, the usual pattern is a plain nullable field with a getter that returns Optional when absence is meaningful to callers.
6Returning Optional<List<T>> Instead of an Empty List
A collection already has a natural way to say "nothing": being empty. Wrapping it in Optional gives the caller two different empty states to handle.
The empty list version is shorter for every caller, can be iterated without any check, and removes the question of what an empty Optional would mean that an empty list does not. The same applies to arrays, maps and streams.
7Using Optional to Replace a Simple Local Null Check
Finally, some code wraps a local value in Optional only to avoid writing an if statement.
Inside a method, where the value never crosses an API boundary, Optional informs nobody, because there is no caller reading the signature. It only adds an object and some indirection. Objects.requireNonNullElse or a simple condition says the same thing directly. This one is a judgement call, and short chains like the one above are sometimes accepted, but as a habit it spreads Optional into places it was not meant for.
8Follow-up questions
Is orElseThrow() really better than get() if both throw the same exception?
Yes, because the name tells the reader that throwing is intended. With get(), a reader cannot tell whether the author checked the value somewhere else or simply forgot. With orElseThrow(), the decision is visible, and we can also pass a supplier to throw a more meaningful exception such as orElseThrow(() -> new UserNotFoundException(id)).
What is the difference between orElse and orElseGet?
orElse(value) always evaluates its argument, even when the Optional holds a value, while orElseGet(supplier) only calls the supplier when the Optional is empty. So orElse(loadDefaultFromDatabase()) runs the database call every time. For constants, orElse is fine; for anything expensive or with side effects, use orElseGet.
Is it ever acceptable to use Optional as a parameter?
It is rarely the best choice. In private helper methods inside a stream pipeline it sometimes reads naturally, but in public APIs overloads, a builder or a @Nullable parameter are clearer and do not force callers to wrap values.
9Conclusion
In this answer, we saw that Optional is meant as a return type that makes absence visible, and that the common anti-patterns all work against that purpose. Calling get() without a check, or pairing isPresent() with get(), brings back the crash or the old null check; returning null from an Optional method breaks the caller's trust; Optional fields, parameters and collection elements add allocation and clutter; Optional<List<T>> duplicates what an empty list already says; and wrapping a local value only to avoid an if statement adds cost without informing anyone.
Open with
Optional is meant to be a return type that tells the caller there may be no result, so most anti-patterns are ways of using it against that purpose.
Then, if they want more
The first is calling get without checking. It throws NoSuchElementException, which is just a null pointer crash with a different name, so I use orElseThrow when absence is a bug, or map and orElse when it is not. The second is isPresent followed by get.
It works, but it is a null check in disguise, and the get can be moved away from its check during later edits; map, ifPresent and orElse are safer. The third is returning null from a method that returns Optional, which crashes callers who trusted the signature; it should return Optional.empty.
The fourth is using Optional for fields, parameters or collection elements. It is not Serializable, JPA does not map it, every instance is an extra allocation, and as a parameter it still allows null to be passed. For fields I keep a nullable field and return Optional from the getter.
The fifth is Optional of a List, which gives two ways to say nothing; an empty list is enough. And the last is wrapping a local value just to avoid an if statement, which costs an object and informs nobody, so a plain condition or Objects.requireNonNullElse is better.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 10, Java 8, Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "List common Optional anti-patterns and explain why each is a problem.": https://scrivio-interview-library.netlify.app/java/optional/a28abcf5a5/