Quick Reference & Example: Optional
Quick Reference
Optional is primarily for use as a return type where there is a clear need to prevent Null Pointer Exception. A variable of type Optional should never itself be null; it should always point to an Optional instance.
Methods in the Optional class include:
empty, filter, flatMap, get, ifPresent, ifPresentOrElse, isPresent, map, of, ofNullable, or, orElse, orElseGet, orElseThrow, stream
An example to demonstrate: flatMap, get, isPresent, map, ofNullable, orElse
Imagine that there is a car dealership offering a promotional package for selected car models. Each Car has a model name and an optional PromoPackage. Each PromoPackage has a promo name and an optional list of promo items.
Output:
------------
Car model: Model A
Car model: Model B
Car model: Not Available
Promo Name: Spring Promo
Promo Name: Dummy Promo
Promo Name: Not Available
Promo Items: [Ambient Lighting, Comfort Access]
Promo Items: [Not Available]
Promo Items: [Not Available]
Optional is primarily for use as a return type where there is a clear need to prevent Null Pointer Exception. A variable of type Optional should never itself be null; it should always point to an Optional instance.
Methods in the Optional class include:
empty, filter, flatMap, get, ifPresent, ifPresentOrElse, isPresent, map, of, ofNullable, or, orElse, orElseGet, orElseThrow, stream
An example to demonstrate: flatMap, get, isPresent, map, ofNullable, orElse
Imagine that there is a car dealership offering a promotional package for selected car models. Each Car has a model name and an optional PromoPackage. Each PromoPackage has a promo name and an optional list of promo items.
class PromoPackage { private String promoName; private Optional<List<String>> optItems; PromoPackage (String promoName, Optional<List<String>> optItems) { this.promoName = promoName; this.optItems = optItems; } String getPromoName () {return this.promoName;} Optional<List<String>> getOptItems() {return this.optItems;} }
class Car { private String model; private Optional<PromoPackage> optPromoPackage; Car (String model, Optional<PromoPackage> optPromoPackage) { this.model = model; this.optPromoPackage = optPromoPackage; } String getModel () {return this.model;} Optional<PromoPackage> getOptPromoPackage() {return this.optPromoPackage;} }
public class OptionalCarsTest{ public static void main(String[] args) { List<String> springItems = Arrays.asList ("Ambient Lighting", "Comfort Access"); PromoPackage springPromo = new PromoPackage ("Spring Promo", Optional.ofNullable(springItems)); List<String> dummyItems = null; PromoPackage dummyPromo = new PromoPackage ("Dummy Promo", Optional.ofNullable(dummyItems)); Car car0 = new Car ("Model A", Optional.ofNullable(springPromo)); Car car1 = new Car ("Model B", Optional.ofNullable(dummyPromo)); Car car2 = null; List<Car> carList = Arrays.asList(car0, car1, car2); Consumer<Optional<Car>> checkCar = (o) -> { String carModel = o.isPresent() ? o.get().getModel() : "Not Available"; System.out.println ("Car model: " + carModel); }; carList.forEach(c -> checkCar.accept(Optional.ofNullable(c))); Consumer<Optional<Car>> checkPromoName = (o) -> { String promoName = o.flatMap(Car::getOptPromoPackage) .map(PromoPackage::getPromoName) .orElse ("Not Available"); System.out.println("Promo Name: " + promoName); }; carList.forEach(c -> checkPromoName.accept(Optional.ofNullable(c))); Consumer<Optional<Car>> checkPromoItems = (o) -> { List<String> items = o.flatMap(Car::getOptPromoPackage) .flatMap(PromoPackage::getOptItems) .orElse(Arrays.asList("Not Available")); System.out.println("Promo Items: " + items); }; carList.forEach(c -> checkPromoItems.accept(Optional.ofNullable(c))); } }
Output:
------------
Car model: Model A
Car model: Model B
Car model: Not Available
Promo Name: Spring Promo
Promo Name: Dummy Promo
Promo Name: Not Available
Promo Items: [Ambient Lighting, Comfort Access]
Promo Items: [Not Available]
Promo Items: [Not Available]
Comments
Post a Comment