Skip to content

Instantly share code, notes, and snippets.

@ivanursul
Created November 16, 2015 14:06
Show Gist options
  • Save ivanursul/82bcb67bc7fbe7daa02f to your computer and use it in GitHub Desktop.
Save ivanursul/82bcb67bc7fbe7daa02f to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
Insurance insurance = new Insurance("insurance1");
Car car = new Car(Optional.of(insurance));
Person person = new Person(Optional.of(car));
Optional<String> s = person.getCar()
.flatMap(Car::getInsurance)
.map(Insurance::getName);
}
}
class Person {
private Optional<Car> car;
public Person(Optional<Car> car) {
this.car = car;
}
public Optional<Car> getCar() {
return car;
}
}
class Car {
private Optional<Insurance> insurance;
public Car(Optional<Insurance> insurance) {
this.insurance = insurance;
}
public Optional<Insurance> getInsurance() {
return insurance;
}
}
class Insurance {
private String name;
public Insurance(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment