# Pattern match Optional in Java 21

DevFeed: [Pattern match Optional in Java 21](<https://devfeed.tech/articles/pattern-match-optional-in-java-21-22012.md>)

Original publisher: [Read original article](<http://blog.joda.org/2024/02/pattern-match-optional-in-java-21.html>)

Author: Stephen Colebourne (noreply@blogger.com)

Published: 2024-02-20T09:19:00Z

Content type: article

Language: en

Sources: [Stephen Colebourne](<https://devfeed.tech/sources/stephen-colebourne.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [java](<https://devfeed.tech/tags/java.md>), [java21](<https://devfeed.tech/tags/java21.md>), [null](<https://devfeed.tech/tags/null.md>), [optional](<https://devfeed.tech/tags/optional.md>), [pattern-matching](<https://devfeed.tech/tags/pattern-matching.md>)

## AI overview

This article presents a rarely useful technique for pattern matching an Optional in Java 21 by converting its value with orElse(null) and testing it with instanceof. It discusses functional Optional methods, an iterable helper, limitations in Java 17, and a possible use in long if-else chains.

## Source excerpt

I'm going to describe a trick to get pattern patching on Optional in Java 21, but one you'll probably never actually use. Using Optional As of Java 21, Pattern matching in Java allows us to check a value against a type like an instanceof with a new variable being declared of the correct type. Pattern matching can handle simple types and the deconstruction of records. But pattern matching of arbitrary classes like Optional is not yet supported. (Work to support pattern match methods is ongoing). In normal code, the best way to use Optional is with one of the functional methods: var addressOpt = findAddress(personId); var addressStr = addressOpt .map(address -> address.format()) .orElse("No address available"); This works well in most cases. But sometimes you want to use the Optional with a return statement. This results in code using get() like this: var addressOpt = findAddress(personId); if (addressOpt.isPresent()) { // early return if address found return addressOpt.get().format(); } // lots of other code to handle case when address not found One way to improve this is to write a simple method: /** * Converts an optional to an iterable for use in the for-each statement. * * @param &ltlT> the type of optional element * @param optional the optional * @return an iterable representation of the optional */ public static &ltlT> Iterable&ltlT> inOptional(Optional&ltlT> optional) { return optional.isPresent() ? List.of(optional.get()): List.of(); } Which allows the following neat form: for (var address : inOptional(findAddress(personId))) { // early return if address found return address.format(); } // lots of other code to handle case when address not found This is a great approach providing that you don't need an else branch. Using Optional with Pattern matching With Java 21 and pattern matching we have a new way to do this! if (findAddress(personId).orElse(null) instanceof Address address) { // early return if address found return address.format(); } else { // lots of