I was going through a tutorial of Optional class here - https://www.geeksforgeeks.org/java-8-optional-class/ which has the following
String[] words = new String[10];
Optional<String> checkNull = Optional.ofNullable(words[5]);
if (checkNull.isPresent()) {
String word = words[5].toLowerCase();
System.out.print(word);
} else{
System.out.println("word is null");
}
I am trying to make it of less lines using ifPresent
check of Optional
as
Optional.ofNullable(words[5]).ifPresent(a -> System.out.println(a.toLowerCase()))
but not able to get the else part further
Optional.ofNullable(words[5]).ifPresent(a -> System.out.println(a.toLowerCase())).orElse();// doesn't work```
Is there a way to do it?
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.
We can replace the multiple null checks using the Optional object's isPresent method. The empty method of the Optional method is used to get the empty instance of the Optional class. The returned object doesn't have any value. The Optional class is present in the java.
Writing with if-else statement is imperative style and it requires the variable car to be declared before if-else block. Using map in Optional is more functional style. And this approach doesn't need variable declaration beforehand and is recommended way of using Optional .
Once you have created an Optional object, you can use the isPresent() method to check if it contains a non-null value. If it does, you can use the get() method to retrieve the value. Developers can also use the getOrElse() method, which will return the value if it is present, or a default value if it is not.
Java-9 introduced ifPresentOrElse
for something similar in implementation. You could use it as :
Optional.ofNullable(words[5])
.map(String::toLowerCase) // mapped here itself
.ifPresentOrElse(System.out::println,
() -> System.out.println("word is null"));
With Java-8, you shall include an intermediate Optional
/String
and use as :
Optional<String> optional = Optional.ofNullable(words[5])
.map(String::toLowerCase);
System.out.println(optional.isPresent() ? optional.get() : "word is null");
which can also be written as :
String value = Optional.ofNullable(words[5])
.map(String::toLowerCase)
.orElse("word is null");
System.out.println(value);
or if you don't want to store the value in a variable at all, use:
System.out.println(Optional.ofNullable(words[5])
.map(String::toLowerCase)
.orElse("word is null"));
For a bit to be more clear ifPresent
will take Consumer
as argument and return type is void
, so you cannot perform any nested actions on this
public void ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
Parameters:
consumer - block to be executed if a value is present
Throws:
NullPointerException - if value is present and consumer is null
So instead of ifPreset()
use map()
String result =Optional.ofNullable(words[5]).map(String::toLowerCase).orElse(null);
print Just to print
System.out.println(Optional.ofNullable(words[5]).map(String::toLowerCase).orElse(null));
If you are using java 9, you can use ifPresentOrElse()
method::
https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#ifPresentOrElse-java.util.function.Consumer-java.lang.Runnable-
Optional.of(words[5]).ifPresentOrElse(
value -> System.out.println(a.toLowerCase()),
() -> System.out.println(null)
);
If Java 8 then look this great cheat sheet :
http://www.nurkiewicz.com/2013/08/optional-in-java-8-cheat-sheet.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With