Sunday, August 4, 2019

Java 8 Optional equals() Method Example

1. Overview

In this tutorial, We'll learn how to compare the Optional value with another value or object. Optional is a java 8 new class and introduced in java.util package.

First, we'll understand the syntax and its meaning. Next, equals() example program and its internal implementation.

optional-equals


Optional class All Methods

API Note: 

Indicates whether some other object is "equal to" this Optional value. The other object is considered equal if it is also an Optional and; both instances have no value present or; the present values are "equal to" each other via equals().

Thumb rule is that the passed object also must be Optional object. Otherwise, this method will return false.



2. equals Syntax


The following is the syntax from java 12 API (latest as of this time).

public boolean equals(Object obj)

This method takes Object as an argument that means any object can be passed to this method. And returns a boolean value. Returns true if the other object is "equal to" this object otherwise false.

This is a non-static method so we should call the equals method on Optional instance.

3. Java 8 equals() Example


We are now writing multiple example programs on equals() method.
Equals() method takes Object as an argument that means we can pass String, StringBuffer, Optional and List or any object.

3.1 Passing String:


Example program on passing String as an argument to equals method.

Optional<String> optional = Optional.ofNullable("Java8");
boolean isEqual = optional.equals("Java8");
System.out.println(isEqual);

Output:

false

3.2 Passing List:


Optional<String> optional2 = Optional.ofNullable("Java8");
List<String> list = Arrays.asList("Java8");
boolean isListEqual = optional2.equals("Java8");
System.out.println(isListEqual);

Output:

false

3.3 Passing Optional:


Optional<String> optional3 = Optional.ofNullable("Java8");
Optional<String> optional4 = Optional.ofNullable("Java8");
boolean isOptionalEqual = optional3.equals(optional4);
System.out.println(isOptionalEqual);

Output:

true

Now it returned true because it checks contents equal or not if and if only argument type if Optional.

3.4 If two optional values are different:


Optional<String> optional6 = Optional.ofNullable("Java8");
Optional<String> optional7 = Optional.ofNullable("Java12");
boolean isOptionalNotEqual = optional6.equals(optional7);
System.out.println(isOptionalNotEqual);

Output:

false

3.5 Passing same optional object:


// Passing Same Optional
Optional<String> optional5 = Optional.ofNullable("Java8");
boolean isOptionalSameEqual = optional5.equals(optional5);
System.out.println(isOptionalSameEqual);

Output:

true

4. Internal Implementation


Below is the internal code of how equals() method is implemented. Actually, this is an overridden method from Object class.

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }

    if (!(obj instanceof Optional)) {
        return false;
    }

    Optional<?> other = (Optional<?>) obj;
    return Objects.equals(value, other.value);
}

A) First checks the instance of the same optional. if yes, returns true.
B) If the passed object is not an instance of Optional. That means if we pass String or List then it is not Object of Optional. So, in this case, it returns false.
C) If execution comes this part then object must be an Optional instance. Now, It will compare two optional values calling Objects.equals(value, other.value) method by passing two values. If values are equals it returns true, otherwise false.

5. Conclusion


In this article, We've learned about Java 8 new Optional equals() method. This method compares the given object with the current optional value. If the same returns true, otherwise false.

Shown different example programs on each case.

A step by step guide to its internal implementation and how it works.

All the code shown is over GitHub.
API Ref

No comments:

Post a Comment