public class Product {
private String name;
private List<Image> images;
//getters and setters
public class Images {
private String url;
private List<Thumbnail> thumbnails;
// getters and setters
public class Thumbnail{
//getters and setters
private String url;
}
}
}
}
I have this class. I need to fetch product name, url of the first image and url of the first thumbnail. I also need to make sure product, images and thumbnails are non-empty.
This is how I am trying to do it:
Optional.ofNullable(product).ifPresent(productData -> {
response.setProductName(productData.getName());
Optional.ofNullable(productData.getImages()).ifPresent(images -> {
images.stream().findFirst().ifPresent(image -> {
response.setImageUrl(image.getUrl());
Optional.ofNullable(image.getThumbnails()).ifPresent(thumbnails -> {
thumbnails.stream().findFirst().ifPresent(thumbnail -> {
response.getThumbnailUrl();
});
});
});
});
});
Is there a better way?