Optional
to help developers handle NullPointerException
.NullPointerException
and in many cases, this very unpleasant exception makes debugging code to understand where some of your predecessors (and possibly you) did not put the notorious null
test.null
? Naturally, Java does not forbid us to do this, but with Optional
it becomes a bit more convenient and clearer.Optional
: import java.util.Date; import java.util.Optional; public class Person { private Optional<String> firstName; private Optional<String> secondName; private Optional<Integer> age; private Optional<PersonAddress> address; public Optional<String> getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = Optional.ofNullable(firstName); } public Optional<String> getSecondName() { return secondName; } public void setSecondName(String secondName) { this.secondName = Optional.of(secondName); } public Optional<Integer> getAge() { return age; } public void setAge(Integer age) { this.age = Optional.ofNullable(age); } public Optional<PersonAddress> getAddress() { return address; } public void setAddress(PersonAddress address) { this.address = Optional.of(address); } }
Optional - of(), ofNullable())
class Optional - of(), ofNullable())
.Optional
, below are examples of how to create objects: /** Optional */ // Optional Optional<Person> optionalPerson = Optional.empty(); //Optional Optional<Person> optionalNonNull = Optional.of(somePerson); //Optional Optional<Person> optionalNullable = Optional.ofNullable(somePerson);
null
objects that are transmitted or processed in various methods takes up many lines of code if you need to work not only with the transferred object, but with the object field, which in turn contains another field, for example, a text description.null
we get a NullPointerException
, so first we need to check each object for null
and then take the text field we need: Person person = getDefaultPerson(); if (person != null) { PersonAddress personAddress = person.getAddress(); if (personAddress != null) { PersonAddressStreet street = personAddress.getStreet(); if(street != null) { streetName = street.getStreetName(); } else { streetName = "EMPTY"; } } }
Optional
: String streetName = person.flatMap(Person::getAddress) .flatMap(PersonAddress::getStreet) .map(PersonAddressStreet::getStreetName) .orElse("EMPTY");
ifPresent()
method also allows you to eliminate some redundancy of code like the following: if(person != null) { System.out.println(person); }
Optional
: person.ifPresent(System.out::println);
isPresent()
does not provide us with great benefits in eliminating code redundancy, but it gives a little more information to written code. if(person != null) { System.out.println(person) } else { System.out.println("Person not found!"); }
Optional
: if (person.isPresent()) { System.out.println(person.get()); } else { System.out.println("Person not found!"); }
Person personNew = person != null ? person : new Person();
Optional
: Person personNew = person.orElse(new Person());
Person personNewThrow = person.orElseThrow(Exception::new);
Optional
does not give any guarantee of getting rid of NullPointerException
and all null
checks could have been described before, but with Optional
these actions become faster and simpler, as additional methods for checking an object or checking and some further actions with an object are already described and we just have to use them in our code.Optional
helps to make the code more informative and make it more readable.Source: https://habr.com/ru/post/225641/
All Articles