private List<Wizards> getWizards(){
return List.of(
new Wizards("Gryfindor","phoenix feather core","Half Blood","Male","Harry Potter"),
new Wizards("Gryfindor","unicorn hair core","Pure Blood","Male","Ronald Wisely"),
new Wizards("Gryfindor","dragon heart string core","Mud Blood","Female","Hermione Granger"),
new Wizards("Slytherin","unicorn hair core","Pure Blood","Male","Draco Malfoy"),
new Wizards("Ravenclaw","unicorn hair core","Pure Blood","Male","Cedric Diggory")
);
}
- Empty Creation
- Create with Static Method of
- ofNullable
- If Present
- Or Else
- orElseGet
- orElseThrow
- Get
- Map
- Filter
To create an empty Optional object, we simply need to use its empty static method
Optional<Wizard> empty = Optional.empty();
System.out.println(empty.isPresent());
We can also create an Optional object with the static method of:
Optional<Wizard> wizard = Optional.of(getWizards().get(0));
System.out.println(wizard.isPresent());
in case we expect some null values, we can use the ofNullable() method:
Optional<Wizard> wizard2 = Optional.ofNullable(null);
System.out.println(wizard2.isPresent());
The ifPresent() method enables us to run some code on the wrapped value if it's found to be non-null. Before Optional, we'd do:
empty.ifPresent(name-> System.out.println(empty.get().getName()));
wizard.ifPresent(name-> System.out.println(wizard.get().getName()));
The orElse() method is used to retrieve the value wrapped inside an Optional instance. It takes one parameter which acts as a default value. The orElse() method returns the wrapped value if it's present and its argument otherwise:
wizard1 = Optional.ofNullable(wizard_null)
.orElse(getWizards().get(1));
System.out.println(wizard1.getName());
The orElseGet() method is similar to orElse(). However, instead of taking a value to return if the Optional value is not present, it takes a supplier functional interface which is invoked and returns the value of the invocation:
Wizard wizard_null = null;
Wizard wizard1 = Optional.ofNullable(wizard_null)
.orElseGet(() -> {
// n number of complicated lines
return getWizards().get(2);
});
System.out.println(wizard1.getName());
The orElseThrow() method follows from orElse() and orElseGet() and adds a new approach for handling an absent value. Instead of returning a default value when the wrapped value is not present, it throws an exception:
wizard1 = Optional.ofNullable(wizard_null)
.orElseThrow(
IllegalAccessError::new
);
The final approach for retrieving the wrapped value is the get() method:
wizard.ifPresent(name-> System.out.println(wizard.get().getName()));
transform the Optional value with the map() method:
Optional<List<Wizard>> listOptional = Optional.of(getWizards());
Integer listSize = listOptional.map(List::size)
.orElse(0);
System.out.println(listSize);
We can run an inline test on our wrapped value with the filter method. It takes a predicate as an argument and returns an Optional object. If the wrapped value passes testing by the predicate, then the Optional is returned as-is.
System.out.println(wizard.filter(y-> y.getHouse().equals("Gryfindor")).isPresent());
System.out.println(listOptional.filter(wizards -> wizard.get().getHouse().equals("Gryfindor")).get());
Let's try ifPresentOrElse out by not only providing a publish method, but also an alternative Runnable function that will create a notification saying that the article wasn't published.
wizard.ifPresentOrElse(
this::publish,
this::notifyThatNothingWasPublished
);
This method is enabling us to provide a supplier with an alternative Optional that will be returned if the original Optional is empty.
Optional<Wizard> orWizard = getWizard().or(() -> Optional.of(getWizards().get(4)));
System.out.println(orWizard.get().getName());
What this method does is to convert our Optional into a Stream, meaning that if the Optional contains a value it'll become a Stream of one element. If the Optional is empty, it'll create an empty stream.
Stream<Wizard> wizardStream = Stream.concat(
orWizard.stream(), getWizardStream()
);
This is a quite similar method to the Optional.orElseThrow(exceptionSupplier), which allows us to define the exception by wrapping it in a Supplier.
getWizard().orElseThrow(IllegalStateException::new);