Optional equals() method in Java with Examples
The equals() method of java.util.Optional class in Java is used to check for equality of this Optional with the specified Optional. This method takes an Optional instance and compares it with this Optional and returns a boolean value representing the same.
public boolean equals(Object obj)
Parameter: This method accepts a parameter obj which is the Optional to be checked for equality with this Optional.
Return Value: This method returns a boolean which tells if this Optional is equal to the specified Object.
Exception: This method do not throw any Exception.
Optional 1: Optional Optional 2: Optional Comparing Optional 1 and Optional 2: true
Optional 1: Optional Optional 2: Optional.empty Comparing Optional 1 and Optional 2: false
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.