in

Be careful Of This Java Optional Method




Let’s Remember Java Optional 🤓

According to Oracle it’s “A container object which may or may not contain a non-null value.”
Optional was introduced in Java 8 and has been used by the SpringBoot team in many projects.


The most common usage of Optionals is in the Spring Data project. Let’s look at the JpaRepository interface and an example method.
Say we have a User entity with an Id type of integer and that we have a JpaRepository for it

@Repository  
public interface IUserRepo extends JpaRepository<User, Integer>  
{  
    Optional<User> findByUserName(String userName);  
}
Enter fullscreen mode

Exit fullscreen mode

We defined a method that searches for a user via their user name and returns an Optional of a User.




Optional’s Convenience Methods 🙌

Optional comes in with many method meant to enable us to write clean and readable code.

  • map(..).or(…)
  • map(…).orElse(…)
  • check out Oracle’s docs for the full list.

However, there is one method with a dangerously unexpected behavior




Meet The orElse Method 👀

According to Oracle’s doc:

public T orElse(T other) 

Return the value if present, otherwise return other.

Now, we can add a method call as the parameter of the orElse, which will be run if the Optional is empty, right?

Yes, that’s correct, BUT, what if I tell you that it will run anyways regardless of the presence of the value in Optional or not.


Let’s test it ✍️

@Test  
public void orElseTest()  
{  
    String result = Optional.of("hello").orElse(someMethod());  
    assertThat(result).isEqualTo("hello");  
}  
private String someMethod()  
{  
    System.out.println("I am running !!");  
    return "hola";  
}
Enter fullscreen mode

Exit fullscreen mode

The test does pass, but we notice that on the console we have the string “I am running” printed out.




Why is that? 🤨

  • Java runs the methods and caches its result in case it’s to be returned for performance reasons.



So Be Careful ⛔️

We want to be careful if the method inside the orElse might have a side effect, because it will be run anyways.




👉🏾 Also check out How to escape NullPointerExceptions in Java using Optional



Source: https://dev.to/jarjanazy/be-careful-of-this-java-optional-method-439

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

GIPHY App Key not set. Please check settings

Vodafone Idea Might Raise Rs 7540 Crore Within Three Months

Make My Meal App Built Using Flutter