Part 1: Java 8 ~ for Each and Lambda Expression ~ Part 2: Java 8 ~ Stream API ~ to start now The third: Java8 to start now-date and time API-
Optional is a class that handles things that may or may not have non-null values.
ʻOptional
Optional emptyCase = Optional.empty();
Generate an empty Optional
Optional presentCase = Optional.of("example");
Generate Optional with value If you pass null as an argument, you will get a NullPointerException
Optional nullableNullCase = Optiona.ofNullable(null);
Optional nullableNotNullCase = Optiona.ofNullable("example");
Generates an empty Optional if the argument is null, otherwise creates an Optional with the argument value
isPresent Returns true if it has a value, false otherwise
Optional presentCase = Optional.of("example");
presentCase.isPresent(); // true
Optional emptyCase = Optional.empty();
emptyCase.isPresent(); // false
ifPresent If it has a value, it executes the argument Consumer with the value as an argument, otherwise it does nothing.
Optional presentCase = Optional.of("example");
presentCase.ifPresent(s -> {System.out.println(s)}); //To standard output"example"And output
Optional emptyCase = Optional.empty();
emptyCase.ifPresent(s -> {System.out.println(s)}); //do nothing
get
If it has a value, it returns that value, otherwise it throws a NoSuchElementException
Optional presentCase = Optional.of("example");
presentCase.get(); // example
Optional emptyCase = Optional.empty();
emptyCase.get(); //NoSuchElementException occurs
Use if you expect to have a value If you don't have a value and want to use the default value, you should use the following ʻor Else`
orElse If it has a value, it returns that value, otherwise it returns the value specified by the argument.
Optional presentCase = Optional.of("example");
presentCase.orElse("default"); // example
Optional emptyCase = Optional.empty();
emptyCase.orElse("default"); // default
orElseGet If it has a value, it returns that value, otherwise it returns the result of the Supplier specified by the argument.
Optional presentCase = Optional.of("example");
presentCase.orElseGet(() -> {"default"}); // example
Optional emptyCase = Optional.empty();
emptyCase.orElseGet(() -> {"default"}); // default
The difference from ʻorElse` is whether you pass the default value directly or express it in a lambda expression.
orElseThrow If it has a value, it returns that value, otherwise it throws an exception with the Supplier specified by the argument.
map If it has a value, it applies the argument mapping function to that value and returns an Optional that describes the result if the result is not null. Otherwise it returns an empty Optional
Optional<String> presentCase = Optional.of("example,1,exp");
Optional<String[]> result = presentCase.map(name -> name.split(",")); // [example, 1, exp]
Optional<String> emptyCase = Optional.empty();
Optional<String[]> result = emptyCase .map(name -> name.split(",")); //Empty Optional
filter Works like a Stream API filter Returns the original Optional if the argument lambda expression condition is met, otherwise returns an empty Optional
Optional<String> presentCase =Optional.of("example");
Optional<String> match = presentCase.filter(exp -> exp.length() == 7); // "example"
Optional<String> notMatch = presentCase.filter(exp -> exp.length() != 7); //Empty Optional
Recommended Posts