Admin

Java 8 Optional : Check value in optional object

Introduction

Optional class was added in Java 8 release under java.util package. It works as container or wrapper for actual value which may or may not be null. This helps to avoid and handle null pointer exception in cleaner way. It also provides methods to check value in optional object. If you are new to Optional please visit Java 8 Optional page.

A noteworthy point about Optional class is that it has private constructor. We can not create object using new keyword, for more details  visit creating optional object page.

While writing code most of the times we need to filter or check value in optional object. Some times we need to check if optional contains the expected value or not, we can check if that in mainly 2 ways 

Syntax

  1. Using public boolean isPresent() method:

    public method which returns true or false depending on value present or not

    Optional<String> emptyOptional = Optional.empty();
    System.out.println(emptyOptional.isPresent());
    Output : false
    Optional<String> nameOptional = Optional.of(“stacktraceguru”);
    System.out.println(nameOptional.isPresent());
    Output : true
  2. Using public Optional<T> filter(Predicate<? super T> predicate) method:

    This method takes a predicate as input parameter. Predicate here is a condition that is checked with respect to optional object. It returns the optional object with value if condition matches else returns empty optional

    Check following examples for more details,

    Optional nameOptional = Optional.of(“stacktraceguru”);
    Optional output = nameOptional.filter(value -> value.equals(“stacktraceguru”));
    Output : Optional[stacktraceguru]

    In above example we try to check optional contains the value as “stacktraceguru”. As this is correct the output is optional with “stacktraceguru” as value.

    Optional nameOptional = Optional.of(“stacktraceguru”);
    Optional output = nameOptional.filter(value -> value.equals(“Java 8”));
    Output : Optional.empty

    In above example we try to check if optional contains the value as “Java 8”. As this is not correct the output is empty optional with no value.

    We can have any predicate function for filter which should return Boolean value, lets see some more filter examples for better understanding

    Optional nameOptional = Optional.of(“stacktraceguru”);
    Optional output = nameOptional.filter(value -> value.length() > 5));
    Output : Optional[stacktraceguru]

    In above example we try to check if optional value has length greater than 5. As this is true the output is optional with “stacktraceguru” as value

    Optional nameOptional = Optional.of(“stacktraceguru”);
    Optional output = nameOptional.filter(value -> value.length() > 15));
    Output : Optional.empty

    In above example we try to check if optional value has length greater than 15. As this is not correct the output is empty optional with no value.

Fast track reading :

  • isPresent() used to check if value present in optional or not
  • filter(…) used to filter value by some condition
  • filter(…) returns the optional of value if condition matches else returns empty optional
  • We can have any predicate function for filter which should return Boolean value

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.