Qick info about Optional
Optional isn't meant to be stored as state.
Optional is not meant to be value. It is container for value so that developer can make checking on its value.
Optional is limited mechanism for library method return types where there needed to be a clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors.
For example, you probably should never use it for something that returns an array of results, or a list of results; instead return an empty array or list.
You should almost never use it as a field of something or a method parameter.
Also routinely using it as a return value for getters would definitely be over-use.
Following quote from an Oracle article:
It is important to note that the intention of the Optional class is not to replace every single null reference. Instead, its purpose is to help design more-comprehensible APIs so that by just reading the signature of a method, you can tell whether you can expect an optional value. This forces you to actively unwrap an Optional to deal with the absence of a value.
How to surrive from null pointer exceptions
You don't want to get null value or NullPointerException when using Optional
1. Don't assign null to an optional variable
Optional<Person>person=null;
Solution: useempty()
Optional<Person>person=Optional.empty();
2. Never callget()
directly to get the value
Optional<Person>person=PersonService.getPerson();PersonmyPerson=persion.get();
Solution: check value withisPresent()
before callingget()
Optional<Person>person=PersonService.getPerson();if(person.isPresent()){PersonmyPerson=persion.get();}PersonmyPerson=persion.get();
What to return when there is no value present
1. Don't use isPresent()-get() to return default value
if(status.isPresent()){returnstatus.get();}else{return"UNKNOWN";}
Solution: useorElse()
returnstatus.orElse("UNKNOWN");
2. Don't useorElse()
to return computed value
// it is called even if "status" is not emptyreturnstatus.orElse(computeStatus());
Solution: useorElseGet()
// computeStatus() is called only if "status" is emptyreturnstatus.orElseGet(this::computeStatus);
3. UseorElseThrow()
to throw a exception
returnstatus.orElseThrow(Exception::new);
How to consume optional values
1. UseifPresent()
to consume value orisPresentOrElse()
to handle empty case
status.ifPresent(System.out::println);status.isPresentOrElse(System.out::println,()->System.out.println("Status not found"));
2. Useor()
to return other Optional
returnstatus.or(()->Optional.of("PENDING"));
3.orElse()
/orElseThrow()
goes nicely with streams and lamdbas (don't break a chain)
returnproducts.stream().filter(e->e.getPrice()>200).findFirst().map(Product:getName).orElse("Not found");
Optional<Cart>cart=...;Productproduct=...;returncart..filter(e->e.getItems().contains(product)).orElseThrow();
Anti-patterns
1. Don't overuse Optional by chaining its methods for purpose of getting value
Statusstatus=...;returnOptional.ofNullable(status).orElse("Pending");
Solution
Statusstatus=...;returnstatus!=null?status:"Pending";
2. Don't use Optional to return empty Collections or Arrays
Solution: Rely onCollections.emptyList()
returnitems==null?Collections.emptyList():items;
3. Don't use Optional as element in Collections or Maps
4. Avoid boxing and unboxing use non-generic Optional
OprionalIntprice==OprionalInt.of(50);OprionalLongprice==OprionalLong.of(50L);
5. When designing APIs don't declare filed of type Optional
it is not value itself
doesn't implement Serializable. It is not intended for use as a property of Java Bean
6. Do not use Optional as constructor or method argument.
Solution: make get method return Optional
classPerson{privatefinalStringname;Person(Stringname){this.name=name;}publicOptional<String>getName(){returnOptional.ofNullable(name);}}
Best practices
1. There is no need to Unwrap Optionals for asserting or testing equality
Optional<String>actual=...;Optional<String>expected=...;assertEquals(expected,actual);
2. Reject wrapped values using .filter()
returnpassword.filter(p->p.length()>5).isPresent();
3.Usemap()
orflatMap()
to transform value - no need to useisPresent()
// transform name to uper case if null return Optional.empty()returnlowerName.map(String::toUpperCase);
3.Usestream()
to treat the Optional instance as Stream
List<Prodcut>products=productsId.stream().map(this::fetchProductById).flatMap(Optional::stream).collect(toList());Optional<Product>fetchProductById(Stringid){// implementation}
Top comments(2)
For further actions, you may consider blocking this person and/orreporting abuse