Java 8 has introduced java.util.Optional class to find a value present or absent.
Normally Java developers do “not null” check to identify whether the Object is null or to be used further for business logic. In a Enterprise application, lot of null checks present as each meaning object is checked, which makes the code messy.
Java 8 introduced Optional package so developers can develop neat code in developing any API or enterprise applications.
Optional class avoid any Null Pointer exceptions since Optional always check and returns empty if not present.
Ways of creating Optional Object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.tutorialflow.example; import java.util.Optional; public class OptionalExample { public static void main(String[] args) { String electricCarName = "Tesla"; String carName = null; Optional<String> electricCar = Optional.of(electricCarName); Optional<String> unknownCar = Optional.ofNullable(carName); Optional<String> emptyObject = Optional.empty(); //we can always check whether it has value.. if(electricCar.isPresent()) { System.out.println(electricCar.get()); } else { System.out.println("No Value in electric Car Object"); } //we can always check whether it has value.. if(unknownCar.isPresent()) { System.out.println(unknownCar.get()); } else { System.out.println("No Value for Unknown Car Object"); } //we can always check whether it has value.. if(emptyObject.isPresent()) { System.out.println(emptyObject.get()); } else { System.out.println("No Value for emptyObject"); } } } |
Output:
Tesla
No Value for Unknown Car Object
No Value for emptyObject
Map & flatMap
Optional also provide methods like flatMap() and map() methods to fetch internal Object values.
Optional.map
if the function returns the plain object
Optional.flatMap if the function returns an Optional Object
1 2 |
System.out.println(employee.map(Employee::getSalary)); System.out.println(employee.flatMap(Employee::getRank)); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.tutorialflow.example; import java.util.Optional; public class OptionalFlatMapExample { public static void main(String[] args) { Employee employee = new Employee(); Salary salary = new Salary(); salary.setNumber(Optional.of(2500)); employee.setSalary(Optional.of(salary)); System.out.println("-------------"); Optional.of(employee).flatMap(Employee::getSalary).ifPresent(inp->System.out.println(inp.getNumber().get())); System.out.println("-------------"); Employee employee2 = new Employee(); //Even though Salary is not present, however it is Optional Optional.of(employee2).flatMap(Employee::getSalary).ifPresent(inp->System.out.println(inp.getNumber().get())); System.out.println("-------------"); Optional<Employee> rank = Optional.of(employee); System.out.println("Rank:"+rank.map(Employee::getRank).get()); System.out.println("-------------"); } } class Employee { private Optional<Salary> salary = Optional.empty(); public Optional<Salary> getSalary() { return salary; } public void setSalary(Optional<Salary> salary) { this.salary = salary; } public String getRank(){ return "Two"; } } class Salary { private Optional<Integer> number = Optional.empty(); public Optional<Integer> getNumber() { return number; } public void setNumber(Optional<Integer> number) { this.number = number; } } |
Output:
————-
2500
————-
————-
Rank:Two
————-