The Optional class in Java is a container class that represents the presence or absence of a value. It…