Wednesday, July 3, 2024
HomeLanguagesJavaOptional equals() method in Java with Examples

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.

Syntax:

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.

Program 1:




// Java program to demonstrate
// the above method
  
import java.text.*;
import java.util.*;
  
public class OptionalDemo {
    public static void main(String[] args)
    {
  
        Optional<Integer> op1
            = Optional.of(456);
  
        System.out.println("Optional 1: "
                           + op1);
  
        Optional<Integer> op2
            = Optional.of(456);
  
        System.out.println("Optional 2: "
                           + op2);
  
        System.out.println("Comparing Optional 1"
                           + " and Optional 2: "
                           + op1.equals(op2));
    }
}


Output:

Optional 1: Optional[456]
Optional 2: Optional[456]
Comparing Optional 1 and Optional 2: true

Program 2:




// Java program to demonstrate
// the above method
  
import java.text.*;
import java.util.*;
  
public class OptionalDemo {
    public static void main(String[] args)
    {
  
        Optional<Integer> op1
            = Optional.of(456);
  
        System.out.println("Optional 1: "
                           + op1);
  
        Optional<Integer> op2
            = Optional.empty();
  
        System.out.println("Optional 2: "
                           + op2);
  
        System.out.println("Comparing Optional 1"
                           + " and Optional 2: "
                           + op1.equals(op2));
    }
}


Output:

Optional 1: Optional[456]
Optional 2: Optional.empty
Comparing Optional 1 and Optional 2: false

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#equals-java.lang.Object-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments