com.google.common.base Optional class represents an object that may or may not exist. It is a part of the Google Guava libraries, which contains a set of utilities and extensions to the Java core libraries.
Code Example 1:
Optional optionalInteger = Optional.absent(); if (!optionalInteger.isPresent()) { System.out.println("Value is absent"); }
In this example, Optional.absent() method is used to create an Optional object that does not contain any value. Then, the isPresent() method is used to check if the value is present or absent.
Code Example 2:
List names = Arrays.asList("John", "Jane", "Doe", "Smith");
if (firstName.isPresent()) { System.out.println("First name starting with J: " + firstName.get()); } else { System.out.println("No name starting with J found"); }
In this example, a stream of names is created using the Java 8 feature. Then, Optional is used to get the first name starting with the letter 'J' from the stream. If the name is present, it is printed; otherwise, a message indicating that no name was found is printed.
Package Library: com.google.common.base.
Java Optional.absent - 30 examples found. These are the top rated real world Java examples of com.google.common.base.Optional.absent extracted from open source projects. You can rate examples to help us improve the quality of examples.