Search code examples
javajava-8guava

Optional vs Guava Optional


I've been using Optional provided from Guava in my project for a very long time. Now I decided to migrate from Java 7 to Java 8, but since in all over my project I used guava's Optional so far, I have to run through the code and make significant changes. Though I am not yet sure if it's worth to change, or it's better to left everything as is, and just start using Java's Optional from now on.

Are there any benefits or leaks in Java's Optional, is it better than what we have in guava?


Solution

  • API of both classes is not the same. I would not change this by regexp. Three differences which are important in my opinion:

    1. Optional in Guava is abstract which means objects are subclasses. In Java Optional is final which means there are no sub classes.
    2. In guava Optional is Serializable. in Java Optional is not Serializable.
    3. Methods are not exactly the same.